(crawler)
| 165 | print_result(result) |
| 166 | |
| 167 | def interactive_extraction(crawler): |
| 168 | # Passing JavaScript code to interact with the page |
| 169 | cprint("\n🖱️ [bold cyan]Let's get interactive: Passing JavaScript code to click 'Load More' button![/bold cyan]", True) |
| 170 | cprint("In this example we try to click the 'Load More' button on the page using JavaScript code.") |
| 171 | js_code = """ |
| 172 | const loadMoreButton = Array.from(document.querySelectorAll('button')).find(button => button.textContent.includes('Load More')); |
| 173 | loadMoreButton && loadMoreButton.click(); |
| 174 | """ |
| 175 | # crawler_strategy = LocalSeleniumCrawlerStrategy(js_code=js_code) |
| 176 | # crawler = WebCrawler(crawler_strategy=crawler_strategy, always_by_pass_cache=True) |
| 177 | result = crawler.run( |
| 178 | url="https://www.nbcnews.com/business", |
| 179 | js = js_code |
| 180 | ) |
| 181 | cprint("[LOG] 📦 [bold yellow]JavaScript Code (Load More button) result:[/bold yellow]") |
| 182 | print_result(result) |
| 183 | |
| 184 | def multiple_scrip(crawler): |
| 185 | # Passing JavaScript code to interact with the page |
no test coverage detected
searching dependent graphs…