import asyncio
# Instead of BeautifulSoupCrawler let's use Playwright to be able to render JavaScript.
from crawlee.playwright_crawler import PlaywrightCrawler, PlaywrightCrawlingContext
import urllib.parse
async def main(terms: str) -> None:
crawler = PlaywrightCrawler(headless=False)
@crawler.router.default_handler
async def request_handler(context: PlaywrightCrawlingContext) -> None:
# Wait for the collection cards to render on the page. This ensures that
# the elements we want to interact with are present in the DOM.
await context.page.wait_for_load_state("networkidle")
await context.infinite_scroll()
url = f"https://www.youtube.com/results?search_query={urllib.parse.quote(terms)}&sp=EgIwAQ%253D%253D"
await crawler.run([url])
if __name__ == "__main__":
asyncio.run(main("music"))
import asyncio
# Instead of BeautifulSoupCrawler let's use Playwright to be able to render JavaScript.
from crawlee.playwright_crawler import PlaywrightCrawler, PlaywrightCrawlingContext
import urllib.parse
async def main(terms: str) -> None:
crawler = PlaywrightCrawler(headless=False)
@crawler.router.default_handler
async def request_handler(context: PlaywrightCrawlingContext) -> None:
# Wait for the collection cards to render on the page. This ensures that
# the elements we want to interact with are present in the DOM.
await context.page.wait_for_load_state("networkidle")
await context.infinite_scroll()
url = f"https://www.youtube.com/results?search_query={urllib.parse.quote(terms)}&sp=EgIwAQ%253D%253D"
await crawler.run([url])
if __name__ == "__main__":
asyncio.run(main("music"))