【问题标题】:How to get all the 'href' links with the help of using Following siblings in selenium Python如何在 selenium Python 中使用以下兄弟姐妹获取所有“href”链接
【发布时间】:2021-04-16 06:55:48
【问题描述】:

from selenium import webdriver
driver = webdriver.Chrome("C://Users//psingh//AppData//Local//Programs//Python//Python38-32//chromedriver.exe")
driver.get("https://download.cms.gov/nppes/NPI_Files.html")
element = driver.find_element_by_xpath("//tr[./td[./b[text()='Weekly Incremental NPI Files']]]/following-sibling::tr//a")
ele = element.get_attribute("href")
print(ele)
driver.quit()

我想获取“每周增量 NPI 文件”标题下方的所有 4 个“href”链接。使用上面的代码,我只能获得第一条链接。

【问题讨论】:

    标签: python-3.x selenium web-scraping selenium-chromedriver


    【解决方案1】:

    使用带有 sdriver.find_elements_by_xpath 元素获取所有元素,然后循环遍历结果:

    from selenium import webdriver
    driver = webdriver.Chrome("C:\Program Files\ChromeDriver\chromedriver.exe")
    driver.get("https://download.cms.gov/nppes/NPI_Files.html")
    element = driver.find_elements_by_xpath("//tr[./td[./b[text()='Weekly Incremental NPI Files']]]/following-sibling::tr//a")
    ele = [ele.get_attribute("href") for ele in element]
    print(ele)
    driver.quit()
    

    【讨论】:

    • 你得到这个错误,TypeError: 'WebElement' object is not iterable
    • 你用过我的例子吗?输出为:['https://download.cms.gov/nppes/NPPES_Data_Dissemination_120720_121320_Weekly.zip', 'https://download.cms.gov/nppes/NPPES_Data_Dissemination_121420_122020_Weekly.zip', 'https://download.cms.gov/nppes/NPPES_Data_Dissemination_122120_122720_Weekly.zip', 'https://download.cms.gov/nppes/NPPES_Data_Dissemination_122820_010321_Weekly.zip', ''] 确定您将 driver.find_element_by_xpath 更改为 driver.find_elements_by_xpath 并带有 s?
    • 是的,我知道了,我没有更改代码,谢谢
    【解决方案2】:

    您可以使用这些 xpath 轻松获取标签“a”:

    xpath = "\\a[contains(@href,'NPPES Data')]"
    

    它将获取所有具有 href 适应条件的“a”标签。

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 2019-11-20
      • 1970-01-01
      • 2020-05-27
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      相关资源
      最近更新 更多