【问题标题】:How to do a loop on a dynamic href link with selenium in python?python - 如何在python中使用selenium对动态href链接进行循环?
【发布时间】:2019-08-31 07:01:33
【问题描述】:

我想在动态href 上创建一个循环。事实上,我每页下载一组文件。在每一页上,我下载 100 个文本文件,但我必须下载 200 000 个文件。所以,我必须在 2000 年点击下一个按钮。为此,我得到了下一个按钮的 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.

【问题讨论】:

    标签: python selenium dynamic href


    【解决方案1】:

    @나민오 我需要帮助来确定我的下一页按钮的 xpath。我的目标是在 Python Selenium 中循环浏览页面。在此图片上的 URL 页面上查看后,请在下面找到下一页按钮的代码。

    next page button picture after inspect

    我尝试使用 selenium 在 python 中编写以下代码以逐页下载文件。

    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
    

    【讨论】:

      【解决方案2】:

      我无法使用示例链接(我无法注册)

      首先..

      什么是“字符串”

      书号?还是类别号?

      如果它只是随机字符串,我认为你应该找到另一种方式。

      使用ActionChain怎么样?还是driver.execute_script()

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

      【讨论】:

        猜你喜欢
        • 2020-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多