简体   繁体   English

Python 和 selenium - 从网页获取所有链接

[英]Python and selenium - get all links from webpage

I have a script that download all .csv files from a site.我有一个从站点下载所有 .csv 文件的脚本。 It is working ok but I need also to get a list with the urls to .csv files.它工作正常,但我还需要获取包含 .csv 文件的 url 的列表。 The part of code for downloading looks like:下载部分代码如下:

# Download files according to Xpath in table
def downloadfiles(Xpath):
    global browser
    time.sleep(10);
    # Click csv img
    try:
        browser.find_element_by_xpath(Xpath).click()
        global downloadcount
        downloadcount = downloadcount + 1
        return
    # Element not found
    except NoSuchElementException as e:
        print("Error enxontró csv")
    return

Here I think i need to do something: browser.find_element_by_xpath(Xpath).click() instead of click I want to get the link.在这里,我想我需要做一些事情: browser.find_element_by_xpath(Xpath).click()而不是 click 我想获取链接。 The code is made in python 3.6 and selenium.代码是用 python 3.6 和 selenium 编写的。 Xpath is "//*[@id=\\"ctl00_ContentPlaceHolder1_ListViewNodos_ctrl0_ListViewArchivosSIN_ctrl0_linkCSV\\"]" Xpath 是"//*[@id=\\"ctl00_ContentPlaceHolder1_ListViewNodos_ctrl0_ListViewArchivosSIN_ctrl0_linkCSV\\"]"

How can I get the links list to .csv files?如何获取 .csv 文件的链接列表?

You can get the link using get_attribute("href") use below line of code您可以使用get_attribute("href")获取链接,使用下面的代码行

link = browser.find_element_by_xpath(Xpath).get_attribute("href")
print link

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM