Chuyển tới nội dung chính

{/* Trang này được tạo tự động từ SKILL.md của kỹ năng bởi website/scripts/generate-skill-docs.py. Chỉnh sửa nguồn SKILL.md, không phải trang này. */}

cạo

Quét web bằng Scrapling - Tìm nạp HTTP, tự động hóa trình duyệt ẩn, bỏ qua Cloudflare và thu thập thông tin qua CLI và Python.

Siêu dữ liệu kỹ năng

NguồnTùy chọn — cài đặt với
`Hermes skills install official/research/scrapling
`
Đường dẫn

optional-skills/research/scrapling ` | | Phiên bản |

1.0.0 ` | | Tác giả | FEUAZUR | | Giấy phép | MIT | | Nền tảng | Linux, macOS, Windows | | Thẻ |

Web ScrAPIng

, `Browser

, `Cloudflare

, `Stealth

, `Crawling

, Spider | | Kỹ năng liên quan | XPROTECTX35XPROTECTX, XPROTECTX36XPROTECTX |

Tham khảo: đầy đủ SKILL.md

thông tin

Sau đây là định nghĩa kỹ năng đầy đủ mà Hermes tải khi kỹ năng này được kích hoạt. Đây là những gì tác nhân coi là hướng dẫn khi kỹ năng được kích hoạt.

cạo`Scrapling là một khung quét web với tính năng chống bot bỏ qua, tự động hóa trình duyệt ẩn và khung nhện. Nó cung cấp ba chiến lược tìm nạp (HTTP, JS động, tàng hình/Cloudflare) và CLI đầy đủ.

Kỹ năng này chỉ dành cho mục đích giáo dục và nghiên cứu. Người dùng phải tuân thủ luật thu thập dữ liệu địa phương/quốc tế và tôn trọng Điều khoản dịch vụ của trang web.

Khi nào nên sử dụng

  • Quét các trang HTML tĩnh (nhanh hơn các công cụ trình duyệt)
  • Quét các trang được hiển thị bằng JS cần có trình duyệt thực
  • Vượt qua Cloudflare Turnstile hoặc phát hiện bot
  • Thu thập dữ liệu nhiều trang bằng một con nhện
  • Khi công cụ web_extract tích hợp không trả về dữ liệu bạn cần

Cài đặt

pip install "scrapling[all]"
scrapling install

`
``Cài đặt tối thiểu (chỉ HTTP, không có trình duyệt):

`
`bash
pip install scrapling

`
``Chỉ với tự động hóa trình duyệt:

`
`bash
pip install "scrapling[fetchers]"
scrapling install

`

## Tham khảo nhanh

| Tiếp cận | Lớp | Sử dụng Khi nào |
|----------|-------|----------|
| HTTP |

Fetcher
` /
`FetcherSession
` | Trang tĩnh, API, yêu cầu hàng loạt nhanh |
| Năng động |

DynamicFetcher
` /
`DynamicSession
` | Nội dung được hiển thị bằng JS, SPA |
| Tàng hình |

StealthyFetcher
` /
`StealthySession
` | Cloudflare, các trang web được bảo vệ chống bot |
| Nhện |

Spider
` | Thu thập thông tin nhiều trang với liên kết sau |

## Cách sử dụng CLI

### Trích xuất trang tĩnh

`bash
scrapling extract get 'https://example.com' output.md

`
``Với bộ chọn CSS và mạo danh trình duyệt:

`bash
scrapling extract get 'https://example.com' output.md \

--css-selector '.content' \
--impersonate 'chrome'

`

### Trích xuất trang được hiển thị bằng JS

``` bash
scrapling extract fetch 'https://example.com' output.md \

--css-selector '.dynamic-content' \
--disable-resources \
--network-idle

`

### Trích xuất trang được bảo vệ bởi Cloudflare

``` bash
scrapling extract stealthy-fetch 'https://protected-site.com' output.html \

--solve-cloudflare \
--block-webrtc \
--hide-canvas

`

### ĐĂNG Yêu cầu

``` bash
scrapling extract post 'https://example.com/API' output.JSON \

--JSON '\{"query": "search term"}'

`

### Định dạng đầu ra

Định dạng đầu ra được xác định bởi phần mở rộng tệp:
-

.html
` -- HTML thô
-

.md
` -- được chuyển đổi thành Markdown
-

.txt

- văn bản thuần túy
-

.JSON
` /

.JSONl
` -- JSON

## Python: Quét HTTP

### Yêu cầu duy nhất

``` python
from scrapling.fetchers import Fetcher`page = Fetcher.get('https://quotes.toscrape.com/')
quotes = page.css('.quote .text::text').getall()
for q in quotes:
print(q)

`

### Phiên (Cookie liên tục)

`Python
from scrapling.fetchers import FetcherSession`with FetcherSession(impersonate='chrome') as session:
page = session.get('https://example.com/', stealthy_headers=True)
links = page.css('a::attr(href)').getall()
for link in links[:5]:
sub = session.get(link)
print(sub.css('h1::text').get())

`

### ĐĂNG/ĐẶT/XÓA

`Python
page = Fetcher.post('https://API.example.com/data', JSON=\{"key": "value"})
page = Fetcher.put('https://API.example.com/item/1', data=\{"name": "updated"})
page = Fetcher.delete('https://API.example.com/item/1')

`

### Có proxy

`Python
page = Fetcher.get('https://example.com', proxy='http://user:pass@proxy:8080')

`

## Python: Trang động (Kết xuất bằng JS)

Đối với các trang yêu cầu thực thi JavaScript (SPA, nội dung được tải từng phần):

`Python
from scrapling.fetchers import DynamicFetcher`page = DynamicFetcher.fetch('https://example.com', headless=True)
data = page.css('.js-loaded-content::text').getall()

`

### Đợi phần tử cụ thể

`Python
page = DynamicFetcher.fetch(
'https://example.com',
wait_selector=('.results', 'visible'),
network_idle=True,
)

`

### Tắt tài nguyên để tăng tốc

Chặn phông chữ, hình ảnh, phương tiện, bảng định kiểu (nhanh hơn ~ 25%):

`Python
from scrapling.fetchers import DynamicSession`with DynamicSession(headless=True, disable_resources=True, network_idle=True) as session:
page = session.fetch('https://example.com')
items = page.css('.item::text').getall()

`

### Tự động hóa trang tùy chỉnh

`Python
from playwright.sync_API import Page
from scrapling.fetchers import DynamicFetcher`def scroll_and_CLIck(page: Page):
page.mouse.wheel(0, 3000)
page.wait_for_timeout(1000)
page.CLIck('button.load-more')
page.wait_for_selector('.extra-results')

page = DynamicFetcher.fetch('https://example.com', page_action=scroll_and_CLIck)
results = page.css('.extra-results .item::text').getall()

`

## Python: Chế độ ẩn (Anti-Bot Bypass)

Đối với các trang web được Cloudflare bảo vệ hoặc có nhiều dấu vân tay:

`Python
from scrapling.fetchers import StealthyFetcher`page = StealthyFetcher.fetch(
'https://protected-site.com',
headless=True,
solve_cloudflare=True,
block_webrtc=True,
hide_canvas=True,
)
content = page.css('.protected-content::text').getall()

`

### Phiên lén lút

`Python
from scrapling.fetchers import StealthySession`with StealthySession(headless=True, solve_cloudflare=True) as session:
page1 = session.fetch('https://protected-site.com/page1')
page2 = session.fetch('https://protected-site.com/page2')

`

## Lựa chọn phần tử

Tất cả các trình tìm nạp đều trả về đối tượng
`Selector
` bằng các phương thức sau:

### Bộ chọn CSS

`Python
page.css('h1::text').get() # First h1 text
page.css('a::attr(href)').getall() # All link hrefs
page.css('.quote .text::text').getall() # Nested selection

`

### XPath

`Python
page.xpath('//div[@class="content"]/text()').getall()
page.xpath('//a/@href').getall()

`

### Tìm phương pháp

`Python
page.find_all('div', class_='quote') # By tag + attribute
page.find_by_text('Read more', tag='a') # By text content
page.find_by_regex(r'\$\d+\.\d\\{2}') # By regex pattern

`

### Các phần tử tương tự

Tìm các phần tử có cấu trúc tương tự (hữu ích cho danh sách sản phẩm, v.v.):

`Python
first_product = page.css('.product')[0]
all_similar = first_product.find_similar()

`

### Điều hướng

`Python
el = page.css('.target')[0]
el.parent # Parent element
el.children # Child elements
el.next_sibling # Next sibling
el.prev_sibling # Previous sibling

`

## Python: Khung nhện

Để thu thập thông tin nhiều trang với liên kết sau:

`Python
from scrapling.spiders import Spider, Request, Response`class QuotesSpider(Spider):
name = "quotes"
start_urls = ["https://quotes.toscrape.com/"]
concurrent_requests = 10
download_delay = 1`async def parse(self, response: Response):
for quote in response.css('.quote'):
yield {
"text": quote.css('.text::text').get(),
"author": quote.css('.author::text').get(),
"tags": quote.css('.tag::text').getall(),
}`next_page = response.css('.next a::attr(href)').get()
if next_page:
yield response.follow(next_page)

result = QuotesSpider().start()
print(f"Scraped \{len(result.items)} quotes")
result.items.to_JSON("quotes.JSON")

`

### Nhện nhiều phiên

Định tuyến các yêu cầu đến các loại trình tìm nạp khác nhau:

`Python
from scrapling.fetchers import FetcherSession, AsyncStealthySession`class SmartSpider(Spider):
name = "smart"
start_urls = ["https://example.com/"]

def configure_sessions(self, manager):
manager.add("fast", FetcherSession(impersonate="chrome"))
manager.add("stealth", AsyncStealthySession(headless=True), lazy=True)

async def parse(self, response: Response):
for link in response.css('a::attr(href)').getall():
if "protected" in link:
yield Request(link, sid="stealth")
else:
yield Request(link, sid="fast", callback=self.parse)

`

### Tạm dừng/Tiếp tục thu thập thông tin

`Python
spider = QuotesSpider(crawldir="./crawl_checkpoint")
spider.start() # Ctrl+C to pause, re-run to resume from checkpoint

`

## cạm bẫy- **Yêu cầu cài đặt trình duyệt**: chạy
`scrapling install
` sau khi cài đặt pip -- nếu không có nó,
`DynamicFetcher
`
`StealthyFetcher
` sẽ không thành công

- **Hết thời gian**: Thời gian chờ của DynamicFetcher/StealthyFetcher tính bằng **mili giây** (mặc định 30000), thời gian chờ của Trình tìm nạp tính bằng **giây**
- **Bỏ qua Cloudflare**:
`solve_cloudflare=True
` thêm 5-15 giây vào thời gian tìm nạp -- chỉ bật khi cần
- **Sử dụng tài nguyên**: StealthyFetcher chạy trình duyệt thực -- hạn chế sử dụng đồng thời
- **Pháp lý**: luôn kiểm tra robots.txt và ToS của trang web trước khi thu thập dữ liệu. Thư viện này dành cho mục đích giáo dục và nghiên cứu
- **Phiên bản Python**: yêu cầu Python 3.10+