【问题标题】:Selenium only getting last page when scraping multiple static pages to Dataframe将多个静态页面抓取到 Dataframe 时,Selenium 仅获取最后一页
【发布时间】:2020-08-21 02:36:52
【问题描述】:
df_results = pd.DataFrame()

#testing with the for loop instead of while because I don't want to run all pges yet, just first 4
for i in range(0,5):
    element = driver.find_element_by_xpath("//span[@class= 'next fg-button ui-state-default']")
    ActionChains(driver).move_to_element(element).click().perform()
    
    # I've also tried by removing the variable
    df_results = df_results.append(pd.read_html(driver.page_source))
  
df1 = pd.concat([df_results])

#save as csv
df1.to_csv('cds.csv')

我正在尝试通过每个页面中具有相同元素的 1 个表格来抓取多个页面。 (当我写这个时听起来很简单......) 我只是试图将每个页面保存到一个空的数据框。 '''df_results''' 仅从最后一页获取表格行,并且显示相同的行 5 次。所以它成功地浏览了每一页,但没有保存每一行。 我尝试在循环中追加,然后在循环外添加 concat 但它不起作用。

【问题讨论】:

  • 添加 URL 以及从哪里抓取数据。
  • @Vin 这不是我想公开的事情,但我能够解决它。还是谢谢!

标签: python-3.x pandas selenium dataframe web-scraping


【解决方案1】:

解决了!我必须在单击“下一步”的 ActionChains 之后添加一个 sleep(10)。在页面加载之前它点击得太快了,这就是为什么没有保存在数据库中的原因。希望这对其他人有帮助,我到处搜索!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2019-09-19
    相关资源
    最近更新 更多