【问题标题】:How to get rid of block while scraping with Selenium如何在使用 Selenium 刮擦时摆脱阻塞
【发布时间】:2021-07-22 08:57:56
【问题描述】:

我正在尝试使用 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()

有什么技巧可以绕过它吗?

【问题讨论】:

  • 如果页面使用 JavaScript 来添加/显示数据,那么这个 JavaScript 需要几(毫秒)秒来完成它。 Selenium 不知道 JavaScript 在做什么,它不会等待 JavaScript - 所以你可能必须自己使用 time.sleep 来完成它。或使用waits

标签: python selenium webdriver screen-scraping user-agent


【解决方案1】:

有时浏览器加载较晚。所以你在你的代码中添加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()

【讨论】:

  • 我试过了,但现在显示这个错误:selenium.common.exceptions.WebDriverException: Message: Failed to decode response from marionette
  • 这个答案可能对你有帮助:stackoverflow.com/a/55206440/14838504
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 2019-03-09
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 2013-09-21
  • 2022-01-02
相关资源
最近更新 更多