(crawler)
| 182 | print_result(result) |
| 183 | |
| 184 | def multiple_scrip(crawler): |
| 185 | # Passing JavaScript code to interact with the page |
| 186 | cprint("\n🖱️ [bold cyan]Let's get interactive: Passing JavaScript code to click 'Load More' button![/bold cyan]", True) |
| 187 | cprint("In this example we try to click the 'Load More' button on the page using JavaScript code.") |
| 188 | js_code = [""" |
| 189 | const loadMoreButton = Array.from(document.querySelectorAll('button')).find(button => button.textContent.includes('Load More')); |
| 190 | loadMoreButton && loadMoreButton.click(); |
| 191 | """] * 2 |
| 192 | # crawler_strategy = LocalSeleniumCrawlerStrategy(js_code=js_code) |
| 193 | # crawler = WebCrawler(crawler_strategy=crawler_strategy, always_by_pass_cache=True) |
| 194 | result = crawler.run( |
| 195 | url="https://www.nbcnews.com/business", |
| 196 | js = js_code |
| 197 | ) |
| 198 | cprint("[LOG] 📦 [bold yellow]JavaScript Code (Load More button) result:[/bold yellow]") |
| 199 | print_result(result) |
| 200 | |
| 201 | def using_crawler_hooks(crawler): |
| 202 | # Example usage of the hooks for authentication and setting a cookie |
no test coverage detected
searching dependent graphs…