首页 > 解决方案 > 在 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

标签: pythonseleniumselenium-webdriver

解决方案


number_of_pages 的值似乎是 10。

找到另一种方法来找出有多少页。

您可以使用 while 循环来检查“下一页”按钮是否可用,如果是,请继续,否则 - 这是最后一页。

像这样:

while next_button_element.is_displayed():
    // Do the action that is currently in the for loop

推荐阅读