MCPcopy
hub / github.com/unclecode/crawl4ai / main

Function main

docs/examples/language_support_example.py:4–42  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2from crawl4ai import AsyncWebCrawler, AsyncPlaywrightCrawlerStrategy
3
4async def main():
5 # Example 1: Setting language when creating the crawler
6 crawler1 = AsyncWebCrawler(
7 crawler_strategy=AsyncPlaywrightCrawlerStrategy(
8 headers={"Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"}
9 )
10 )
11 result1 = await crawler1.arun("https://www.example.com")
12 print("Example 1 result:", result1.extracted_content[:100]) # Print first 100 characters
13
14 # Example 2: Setting language before crawling
15 crawler2 = AsyncWebCrawler()
16 crawler2.crawler_strategy.headers["Accept-Language"] = "es-ES,es;q=0.9,en-US;q=0.8,en;q=0.7"
17 result2 = await crawler2.arun("https://www.example.com")
18 print("Example 2 result:", result2.extracted_content[:100])
19
20 # Example 3: Setting language when calling arun method
21 crawler3 = AsyncWebCrawler()
22 result3 = await crawler3.arun(
23 "https://www.example.com",
24 headers={"Accept-Language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7"}
25 )
26 print("Example 3 result:", result3.extracted_content[:100])
27
28 # Example 4: Crawling multiple pages with different languages
29 urls = [
30 ("https://www.example.com", "fr-FR,fr;q=0.9"),
31 ("https://www.example.org", "es-ES,es;q=0.9"),
32 ("https://www.example.net", "de-DE,de;q=0.9"),
33 ]
34
35 crawler4 = AsyncWebCrawler()
36 results = await asyncio.gather(*[
37 crawler4.arun(url, headers={"Accept-Language": lang})
38 for url, lang in urls
39 ])
40
41 for url, result in zip([u for u, _ in urls], results):
42 print(f"Result for {url}:", result.extracted_content[:100])
43
44if __name__ == "__main__":
45 asyncio.run(main())

Callers 1

Calls 3

arunMethod · 0.95
AsyncWebCrawlerClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…