如何在无头模式下使用硒刮取此站点?

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

在Mac上以无头模式运行抓取测试程序时,可以获得指定的信息。

请帮助我。

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”设置延迟时间来获取这些标签,但无法获取特定标签。

lihaizheng17 回答:如何在无头模式下使用硒刮取此站点?

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

本文链接:https://www.f2er.com/3153696.html

大家都在问