【问题标题】:Extract Page number from a web page using Python Selenium使用 Python Selenium 从网页中提取页码
【发布时间】:2021-12-05 13:07:14
【问题描述】:

我想获取网站中的总页数。所以我所做的是我试图提取底部的页码来计算总页数,以便我可以使用下一页按钮进行遍历。请在下面找到我尝试过的代码:

totpage=driver.find_element_by_xpath("XPATH of the Page Number Shown in the Bottom of the Page")
print(page.text)

但上面的代码将结果打印为 0 of 0 而不是 1 - 200 of 900

请在下面找到 HTML 代码:

<div class="mat-paginator-range-range-label"> 1 - 200 of 900 </div>

我想在提取数字后进行计算,page = 900/200。

我做错了什么?

有没有其他方法可以做到这一点?,

提前致谢

【问题讨论】:

  • 我建议在获取该 div 之前添加一个延迟,我看到的一些页面第一次显示 0 of 0 然后稍后更新实际值。
  • 您能否通过快照或页面 url 向我们展示您的 xpath 在 HTMLDOM 中匹配了多少个节点?
  • 如何显示快照?
  • 好的,我认为延迟有效...现在我需要计算页面...。

标签: python selenium xpath selenium-chromedriver


【解决方案1】:

因为您能够延迟解决0 of 0 instead of 1 - 200 of 900。问题的另一半可以通过以下逻辑解决。您应该将您拥有的变量(使用 .text 您必须获得 1 - 200 of 900)分配给 actual_count_page

actual_count_page = " 1 - 200 of 900 "
a = actual_count_page.strip().split(' ')
total_page_count = int(a[4])
max_number_count = int(a[2])

toal_number_of_pages = int(total_page_count/max_number_count)  + 1
print(toal_number_of_pages)

【讨论】:

    猜你喜欢
    • 2017-12-04
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多