crawl4ai with Proxies: LLM-Ready Web Scraping at Scale
crawl4ai with Proxies: LLM-Ready Web Scraping at Scale
In today’s data-driven world, web scraping has become an essential tool for gathering information from the internet. For developers working on large-scale projects, especially those involving language models (LLMs), efficient and reliable proxy management is crucial. This guide will walk you through setting up a robust web scraping pipeline using crawl4ai, an async Python library built on Playwright, optimized for LLM pipelines.
Introduction to crawl4ai
crawl4ai is a powerful tool designed for developers who need to scrape websites at scale and extract structured data for use with large language models (LLMs). Unlike traditional HTML parsers, crawl4ai outputs clean Markdown or JSON directly, making it ideal for feeding data into machine learning models without the overhead of parsing raw HTML.
Setting Up Your Environment
Before you start, ensure you have Python installed on your system. You can install crawl4ai and other necessary packages using pip:
pip install crawl4ai playwright
You will also need to install Playwright browser binaries by running:
playwright install
Configuring Proxies with crawl4ai
crawl4ai supports proxy configuration through the AsyncWebCrawler class. Here’s how you can set up a basic proxy configuration:
from crawl4ai.crawlers import AsyncWebCrawler, CrawlerRunConfig
# Define your proxy settings
proxy_settings = {
'server': 'http://host:port',
'username': 'your_username',
'password': 'your_password'
}
# Create a crawler configuration with the proxy settings
crawler_config = CrawlerRunConfig(proxy_config=proxy_settings)
# Initialize the AsyncWebCrawler
crawler = AsyncWebCrawler(crawler_config=crawler_config)
Proxy Rotation Strategy
For large-scale scraping, rotating proxies is essential to avoid being blocked by websites. You can achieve this by subclassing BrowserConfig and overriding the proxy per run.
from crawl4ai.configs import BrowserConfig
class RotatingProxyBrowserConfig(BrowserConfig):
def __init__(self, proxy_list):
self.proxy_list = proxy_list
self.current_proxy_index = 0
async def get_browser_args(self):
proxy = self.proxy_list[self.current_proxy_index]
args = await super().get_browser_args()
args.append(f'--proxy-server={proxy["server"]}')
return args
# List of proxies
proxies = [
{'server': 'http://host1:port', 'username': ..., 'password': ...},
{'server': 'http://host2:port', 'username': ..., 'password': ...}
]
# Initialize with rotating proxy configuration
browser_config = RotatingProxyBrowserConfig(proxies)
crawler_config = CrawlerRunConfig(browser_config=browser_config)
crawler = AsyncWebCrawler(crawler_config=crawler_config)
Extracting Data for LLMs
crawl4ai provides two strategies for data extraction: JsonCssExtractionStrategy and LLMExtractionStrategy. The latter is specifically designed to send cleaned Markdown to the model, saving tokens.
Using JsonCssExtractionStrategy
from crawl4ai.strategies import JsonCssExtractionStrategy
# Define your CSS selectors
selectors = {
'title': '.product-title',
'description': '.product-description'
}
# Initialize the extraction strategy
extraction_strategy = JsonCssExtractionStrategy(selectors=selectors)
# Set the extraction strategy in the crawler configuration
crawler_config.extraction_strategy = extraction_strategy
Using LLMExtractionStrategy
from crawl4ai.strategies import LLMExtractionStrategy
# Initialize the LLM extraction strategy
extraction_strategy = LLMExtractionStrategy()
# Set the extraction strategy in the crawler configuration
crawler_config.extraction_strategy = extraction_strategy
Handling Anti-bot Measures
crawl4ai, being built on Playwright, automatically applies stealth patches to avoid detection by anti-bot systems. Additionally, using residential proxies from providers like Bright Data or SmartProxy can further enhance your scraping efforts.
Running a Batch Crawl
crawl4ai is fully async-native, allowing you to run multiple URLs concurrently with ease.
urls = [
'http://example.com/product1',
'http://example.com/product2',
# Add more URLs as needed
]
# Run the crawler
results = await crawler.arun_many(urls)
Memory Management and Session Reuse
To manage memory effectively, crawl4ai pools browser contexts. This means that changing proxies requires creating a new context.
# Create a new crawler with updated proxy settings for a different session
new_proxy_settings = {
'server': 'http://new_host:port',
'username': 'new_username',
'password': 'new_password'
}
new_crawler_config = CrawlerRunConfig(proxy_config=new_proxy_settings)
new_crawler = AsyncWebCrawler(crawler_config=new_crawler_config)
# Run the new session
new_results = await new_crawler.arun_many(urls)
Conclusion
crawl4ai is a versatile and efficient tool for developers looking to scrape websites at scale and extract data for LLMs. By leveraging async capabilities, proxy management, and advanced extraction strategies, you can build robust scraping pipelines that save time and resources.
If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription: https://proxyforge.dev/#download
For professional or managed proxy services, consider providers like Bright Data or SmartProxy, which offer reliable rotating proxies for production use.
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →Need guaranteed uptime? Try a premium provider