python - 在 python 中使用 selenium 进行导航
问题描述
我正在使用 Python 和 Selenium 抓取这个网站。但它目前只抓取 7 月份的前 10 页,它将下一个按钮的前一个兄弟的页码转换为 int 并单击下一个 number_of_pages - 1 但是在它到达第 10 页后它会停止。
网址 - https://planning.adur-worthing.gov.uk/online-applications/search.do?action=monthlyList
谁能帮我把它刮掉所有的页面?
def pagination( driver ):
data = []
last_element = driver.find_element_by_xpath('//a[ contains( concat( " ", normalize-space( @class ), " "), " next ") ]/preceding-sibling::a[1]')
if last_element is None:
number_of_pages = 1
else:
number_of_pages = int( last_element.text )
# data = [ getData( driver ) ]
data.extend(getData(driver))
for i in range(number_of_pages - 1):
driver.find_element_by_xpath('//a[ contains( concat( " ", normalize-space( @class ), " "), " next ") ]').click()
data.extend( getData( driver ) )
time.sleep(1)
return data
解决方案
number_of_pages 的值似乎是 10。
找到另一种方法来找出有多少页。
您可以使用 while 循环来检查“下一页”按钮是否可用,如果是,请继续,否则 - 这是最后一页。
像这样:
while next_button_element.is_displayed():
// Do the action that is currently in the for loop
推荐阅读
- python - 在 ROS 中发布法线
- r - 我在 oracle sql 中保存了一个 blob。我无法在 R 中阅读
- ansible - 如何在 Ansible 中将分配给变量的值更改为整数
- apache-spark - 如何解密pyspark中的base64列
- go - 根据传递的 interface{} 类型动态设置变量类型
- c++ - VS2019 c++ 导入.lib文件
- r - 如何生成缺失的数据结构以在 R 中对高维数据运行模拟?
- git - git push -f 没有先拉,现在充满了遗憾
- c# - 有人可以详细解释一下索赔在.net核心中的主要作用以及如何从索赔中获取信息吗?
- installation - 如何在 CMake 时找到正确的 ZSH $fpath(Linux 发行版差异)?