【问题标题】:Scraping through multiple pages when url doesn't change当 url 没有改变时抓取多个页面
【发布时间】:2020-06-23 19:50:18
【问题描述】:

我需要浏览(链接)[https://mahabocw.in/safety-kit-benefits-distribution/] 中的所有页面。但是当我移到下一页时,网址不会改变。我尝试使用 selenium,但我被卡住了,因为我不知道如何单击下一页。任何帮助或建议将不胜感激。

到目前为止,我已经实现了以下代码。

from selenium import webdriver
import time

url = "https://mahabocw.in/safety-kit-benefits-distribution/"
driver = webdriver.Chrome()
driver.get(url)

下面是我需要点击的按钮元素

<button type="button" class="ag-paging-button">Next</button>

提前非常感谢。 [1]:https://mahabocw.in/safety-kit-benefits-distribution/

【问题讨论】:

  • 这是因为他们使用http请求刷新页面信息。您必须在 selenium 文档中查找鼠标点击事件或了解 requests 库并重现浏览器请求。
  • 我怎么能在这里使用请求库。你能分享一些代码吗?我是这个领域的菜鸟。

标签: python selenium web-scraping


【解决方案1】:

您需要告诉 selenium 单击下一步按钮。将此添加到您的代码中,看看它是否有效。

next_button = '/html/body/div/div[6]/div/article/div/div/div/div/div[2]/div/div/div[2]/div/div[4]/span[2]/div[3]/button'
click_next = driver.find_element_by_xpath(next_button)
click_next.submit()

根据页面的不同,您可能必须使用 click_next.click() 而不是 .submit()。此外,要获得“next_button”,您只需检查页面上的元素,找到所需的项目,然后单击复制为 xpath。

【讨论】:

  • 代码不起作用。我收到以下错误:NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}
  • 实际上click_next 似乎正确检索了元素,但最后一行导致上述错误。我将其更改为click_next.click(),现在可以使用了。谢谢大佬。
  • 谢谢。对于之后阅读它的任何人,您可以在浏览器中使用 xpath finder 扩展来查找变量 next_button 中的路径
  • 对不起,我现在才看到这个。我相信这取决于页面上的项目如何单击它。当我在一个项目中使用这项工作时,我必须使用 .submit() 但我知道在其他页面上 .click() 也可以使用。很高兴你成功了!
  • 实际上.click() 是为我工作的那个。你可以编辑你的答案。再次感谢。
猜你喜欢
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2017-08-16
  • 2017-12-26
相关资源
最近更新 更多