【问题标题】:How can I scrape this site using selenium on headless mode?如何在无头模式下使用 selenium 抓取该站点?
【发布时间】:2019-11-06 09:07:46
【问题描述】:

我想在 Docker 上的 Ubuntu 上使用 selenium 来抓取该站点的 (https://www.monotaro.com/p/8928/5682/) 信息。 所以,我想在无头模式下使用 chromedriver,但我的脚本无法获取 使用无头模式时指定的信息。

当我在mac上运行不带headless模式的抓取测试程序时,可以得到指定的信息。

请帮帮我。

url = "https://www.monotaro.com/p/8928/5682/"
options = webdriver.chrome.options.Options()
#options.add_argument('--headless') # when I use headless mode, I can't get the information.
#options.add_argument('--disable-gpu')
self.browser = webdriver.Chrome("/.../chromedriver",chrome_options=options)
self.browser.get(url)
self.browser.implicitly_wait(10)

self.html = self.browser.page_source
self.soup = BeautifulSoup(self.html, "html.parser")

brand = self.soup.find("span", class_="itd_brand")
print(brand)
brand = brand.get_text().replace('\n','')
print(brand)

当我在没有头模式的情况下运行这个程序时,我可以获得想要的标签和信息。

<span class="itd_brand">
<a href="/brand/907/"> <strong class="st itd_all_size">TRUSCO</strong>
</a> </span>
 TRUSCO 

但是,我无法使用无头模式获取这些标签。

None
Traceback (most recent call last):
  File "/Users/plugins/webScraper.py", line 82, in <module>
    print(monotaro.GetBrand())
  File "/Users/plugins/webScraper.py", line 59, in GetBrand
    brand = brand.get_text().replace('\n','')
AttributeError: 'NoneType' object has no attribute 'get_text'

我尝试使用“implicitly_wait”设置延迟时间来获取这些标签,但我无法获取特定标签。

【问题讨论】:

  • 那么...你的问题/问题是什么?阅读此stackoverflow.com/help/how-to-ask
  • self.html 保存在文件中并在浏览器中打开以查看您得到的结果。也许你会收到警告或验证码。

标签: python selenium web-scraping


【解决方案1】:

我使用 xvfb 包解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多