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

Function simple_example_with_running_js_code

docs/examples/quickstart_async.py:36–56  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

34 print(result.markdown[:500]) # Print first 500 characters
35
36async def simple_example_with_running_js_code():
37 print("\n--- Executing JavaScript and Using CSS Selectors ---")
38 # New code to handle the wait_for parameter
39 wait_for = """() => {
40 return Array.from(document.querySelectorAll('article.tease-card')).length > 10;
41 }"""
42
43 # wait_for can be also just a css selector
44 # wait_for = "article.tease-card:nth-child(10)"
45
46 async with AsyncWebCrawler(verbose=True) as crawler:
47 js_code = [
48 "const loadMoreButton = Array.from(document.querySelectorAll('button')).find(button => button.textContent.includes('Load More')); loadMoreButton && loadMoreButton.click();"
49 ]
50 result = await crawler.arun(
51 url="https://www.nbcnews.com/business",
52 js_code=js_code,
53 # wait_for=wait_for,
54 bypass_cache=True,
55 )
56 print(result.markdown[:500]) # Print first 500 characters
57
58async def simple_example_with_css_selector():
59 print("\n--- Using CSS Selectors ---")

Callers 1

mainFunction · 0.85

Calls 2

AsyncWebCrawlerClass · 0.90
arunMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…