【问题标题】:Pagination with selenium and python使用 selenium 和 python 进行分页
【发布时间】:2022-01-25 12:37:10
【问题描述】:

我正在尝试使用 selenium 和 python 进行抓取,网页在 javascript 中有一个分页器,当我单击按钮时,我可以看到内容重新加载但是当我尝试获取新的表信息时它是相同的旧表信息,selenium 没有注意到 de DOM 信息发生了变化,我知道过时的 DOM,我只是在寻找解决这个问题的最佳途径

for link in source.find_all('div', {'class': 'company-row d-flex'}):
    print(link.a.text, link.small.text, link.find('div', {'class': 'col-2'}).text)

# Next button (I´ll make an iterator)
driver.find_element_by_xpath('//a[@href="hrefcurrentpage=2"]').click()

# Tried this and doesn't work
# time.sleep(5)

# Here the table change but get the same old info 
for link in source.find_all('div', {'class': 'company-row d-flex'}):
    print(link.a.text, link.small.text, link.find('div', {'class': 'col-2'}).text)  ```

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    我认为即使在延迟之后打开下一页后您也会获得相同的数据,因为您是从现有的 source 获取数据。
    所以,你应该在点击分页后重新阅读、重新加载source,可能会有一些延迟。

    【讨论】:

    • 您总是点击同一个按钮。 //a[@href="hrefcurrentpage=2"]您必须在这里将页码增加到 3、4 等
    • 不,我没有,第一次和第二次打印给我相同的信息,我点击按钮,必须不同,我看到它在浏览器中的变化
    • 看来你必须重新加载source。请参阅我的更新答案。
    猜你喜欢
    • 2017-08-04
    • 2018-06-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多