【发布时间】:2020-07-31 20:45:18
【问题描述】:
我正在尝试抓取一个网站,我正在使用以下代码:
import selenium
titles=[]
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
for i in range(len(links)):
driver.get(links[i])
time.sleep(0.5)
data = driver.find_elements_by_xpath('.//a[@class = "question-hyperlink"]')
titles.append(data[0].text)
我在 google colab 上运行此代码。我得到的问题是循环迭代某些值后数据不存储任何值。如果我重新启动内核并重新运行代码,那么代码对于较早的迭代工作正常,并且在另一个迭代中会出现同样的问题。我很困惑为什么会这样。我尝试了很多东西,但没有任何效果。另外,链接的大小很大,有什么办法可以加快速度吗?
编辑:添加了完整代码的链接: https://colab.research.google.com/drive/1SYIA_SUPYzlR-K9ph4grNem-LbL61uB7?usp=sharing
【问题讨论】:
标签: python selenium selenium-chromedriver