Khám phá Learn Stream About Jokes
INSIDER Tony's Friends — Insider — ~2 playbook/tuần, Discord riêng, tài nguyên dựng sẵn Tham gia →
Stream
Liên kết

Scrapling — Thu thập dữ liệu web thông minh, qua mặt được cả Cloudflare

· github

TL;DR

Scrapling là thư viện Python cho web scraping với khả năng tự thích nghi khi website đổi cấu trúc, bypass anti-bot như Cloudflare Turnstile, và build spider crawl quy mô lớn chỉ vài dòng code. Speed thì nhanh gấp 10-40x so với BeautifulSoup.

Nói đơn giản: Thư viện Python giúp mình lấy dữ liệu từ bất kỳ website nào, kể cả những trang có chống bot. Nó còn tự nhớ vị trí dữ liệu dù trang đổi giao diện.

Bài này dành cho ai?

1. Người cần thu thập dữ liệu web

Vấn đề: Website đổi layout là code lại từ đầu, bị chặn liên tục Khi nào cần: Crawl giá sản phẩm, tin tức, job posts dài hạn Được gì: Code chạy được dù website có thay đổi, không bị block

2. Dev cần automation tool cho workflows

Vấn đề: Phải viết script riêng cho từng trang, xử lý proxy, session thủ công Khi nào cần: Build pipeline thu thập dữ liệu tự động Được gì: Tích hợp sẵn proxy rotation, session management, pause/resume crawl

3. Người xây dựng sản phẩm AI

Vấn đề: Tốn token khi dùng AI để parse HTML thủ công Khi nào cần: RAG pipelines, AI agents cần dữ liệu web Được gì: MCP server tích hợp sẵn, giảm token usage đáng kể

Các điểm chính

1. Adaptive — Element tự tìm lại khi website đổi Scrapling dùng thuật toán similarity để nhớ vị trí element. Đổi CSS? Đổi layout? Thêm bớt div? Chỉ cần thêm adaptive=True, nó tự locate lại data. → Làm gì: Dùng page.css('.product', adaptive=True) cho những data cần thu thập dài hạn.

2. Bypass anti-bot — Qua mặt Cloudflare, Turnstile dễ dàng StealthyFetcher tự động giả mạo browser fingerprint, TLS signature, headers. Cloudflare Turnstile, Incapsula, Akamai đều bị qua mặt out of the box. → Làm gì: Dùng StealthyFetcher.fetch(url, solve_cloudflare=True) cho những trang bị chặn.

3. Spider framework — Crawl quy mô lớn trong vài dòng Định nghĩa spider với start_urls và async parse. Hỗ trợ concurrent requests, per-domain throttling, pause/resume bằng Ctrl+C. Tích hợp sẵn proxy rotation. → Làm gì: Dùng Spider class thay vì viết vòng for thủ công khi crawl nhiều trang.

4. Speed — Nhanh gấp 10-40x so với thư viện phổ biến Benchmark chính thức: Scrapling parse 5000 elements mất 2.02ms, trong khi BeautifulSoup + Lxml mất 1584ms (~784x chậm hơn). Element similarity cũng nhanh gấp 5x so với AutoScraper. → Làm gì: Nếu đang dùng BeautifulSoup mà thấy chậm, thử Scrapling parser thôi cũng đã thấy khác biệt.

5. MCP Server — AI assistance tích hợp sẵn MCP server cho phép AI (Claude, Cursor) gọi Scrapling để extract data trước khi xử lý. Giảm token usage vì AI không phải parse HTML thủ công. → Làm gì: Cài pip install "scrapling[ai]" nếu build AI workflow cần web data.

Quick Start

Cài đặt:

pip install scrapling
pip install "scrapling[fetchers]"  # Nếu cần browser automation
scrapling install  # Download browsers

Dùng thử — Adaptive scraping:

from scrapling.fetchers import StealthyFetcher

# Lấy data, tự qua mặt Cloudflare
page = StealthyFetcher.fetch('https://example.com')

# Extract, adaptive=True để tự tìm lại khi website đổi
products = page.css('.product', adaptive=True)

Dùng Spider cho crawl quy mô lớn:

from scrapling.spiders import Spider, Response

class MySpider(Spider):
    name = "demo"
    start_urls = ["https://example.com/"]
    concurrent_requests = 10
    
    async def parse(self, response: Response):
        for item in response.css('.item'):
            yield {"title": item.css('h2::text').get()}

MySpider().start()
#python #web-scraping #automation #ai-tools #scraping
0:00

Chia sẻ ảnh

Bắt đầu gõ để tìm kiếm...