问题描述
我正在尝试使用 Selenium 抓取网站,但我认为它在很多方面都阻止了这种访问。
显示的错误消息是:“selenium.common.exceptions.NoSuchWindowException: Message: browsing context has been discarded”但有时会显示一个错误,说加载页面的时间已过期
此外,Firefox 在加载此页面时会消耗大量 cpu 和内存。
我已经尝试更改用户代理,或无头运行它,但没有结果。
代码如下:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.bet365.com/#/HO/')
matches = browser.find_elements_by_class_name('him-Fixture')
browser.quit()
有什么绕过它的技巧吗?
解决方法
有时浏览器加载延迟。因此,您在代码中添加了 time.sleep()
函数。
示例:
from selenium import webdriver
import time
browser = webdriver.Firefox()
browser.get('https://www.bet365.com/#/HO/')
time.sleep(5)
matches = browser.find_elements_by_class_name('him-Fixture')
browser.quit()