如何在python中使用selenium在动态href链接上执行循环?

2024-03-29 10:59:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个动态href循环。实际上,我每页下载一组文件。在每一页上,我下载100个文本文件,但我必须下载200000个文件。所以,我必须在2000年点击“下一步”按钮。为此,我得到了next按钮的href地址,但不幸的是,这个链接中有两个对象发生了变化,页码1、2、3等等,还有一个字符串。请参阅附件中的下一个按钮的变化样本。你知道吗

https://search.proquest.com/something/E6981FD6D11F45E8PQ/2?accountid=12543#scrollTo

https://search.proquest.com/something/E6981FD6D11F45E8PQ/3?accountid=12543#scrollTo

https://search.proquest.com/something/61C27022597C4092PQ/4?accountid=12543#scrollTo

https://search.proquest.com/something/E431552DC6554BF7PQ/5?accountid=12543#scrollTo

我是Python的新用户。我的水平不好。你知道吗

#Before I add selenium setup for scraping. 

n=2000

for i in range(1,n):
    href="https://search.proquest.com/something/715376F5A5AF44BBPQ/" + str(i) + "?accountid=12543#scrollTo"
    driver.get(href)

#Here, I add the code which allows downloading for each page.

Tags: 文件httpscomaddforsearch动态按钮
2条回答

@나민오我需要帮助识别下一页按钮的xpath。我的目标是在Python中循环浏览页面。请在下面找到下一页按钮的代码后,在这个图片的URL页面上检查。你知道吗

next page button picture after inspect

我尝试用python和selenium编写以下代码来逐页下载文件。你知道吗

while True:

scraping()          # here I call my function that allows to download the files per page

try:
    #Checks if there are more pages with links
    next_link = driver.find_element_by_xpath("//*[@title='Page suivante']")
    drive.execute_script("arguments[0].scrollIntoView();", next_link)
    next_link.click()
     #Time sleep
    time.sleep(20)  
except NoSuchElementException:
    pages_rows= False

示例链接对我不可用(我无法注册)

首先。。你知道吗

what is "string of chacracters"?

书号?或者分类号?你知道吗

如果只是随机串,我想你应该另找方法。你知道吗

使用ActionChain怎么样?或者driver.execute_script()?你知道吗

首先,在我看来,找到字符串的意义(来自.js或.html)更为重要。你知道吗

相关问题 更多 >