【问题标题】:Python Selenium How to get anchor tag href value only if anchor tag contains certain attribute valuePython Selenium如何仅在锚标签包含某些属性值时获取锚标签href值
【发布时间】:2021-02-12 05:44:21
【问题描述】:

我想从GitHub search results 获取 GitHub 存储库链接。现在,我的代码获得了用户名和存储库的链接。如何通过定位锚标记属性值仅获取存储库链接。

我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

path = "C:\programs\chromedriver.exe"
driver = webdriver.Chrome(path)
url = 'https://github.com/topics/flutter-apps'

driver.get(url)

links_list = []

headings = driver.find_elements_by_class_name('f3')


for heading in headings:
    links = heading.find_elements_by_tag_name('a')
    for l in links:
        links_list.append(l.get_attribute('href'),)


print(links_list)

这是我想从中获取链接的代码。

    <h1 class="f3 text-gray text-normal lh-condensed">
      <a data-hydro-click="{&quot;event_type&quot;:&quot;explore.click&quot;,&quot;payload&quot;:{&quot;click_context&quot;:&quot;REPOSITORY_CARD&quot;,&quot;click_target&quot;:&quot;OWNER&quot;,&quot;click_visual_representation&quot;:&quot;REPOSITORY_OWNER_HEADING&quot;,&quot;actor_id&quot;:49521558,&quot;record_id&quot;:484656,&quot;originating_url&quot;:&quot;https://github.com/topics/ios&quot;,&quot;user_id&quot;:49521558}}"
        data-hydro-click-hmac="7b69680b468dda1b4e10ddab19c8034fd4c530bc57957662d8be320d79cc38f1"
        data-ga-click="Explore, go to repository owner, location:explore feed" href="/vsouza">
        vsouza
      </a> /
      <a data-hydro-click="{&quot;event_type&quot;:&quot;explore.click&quot;,&quot;payload&quot;:{&quot;click_context&quot;:&quot;REPOSITORY_CARD&quot;,&quot;click_target&quot;:&quot;REPOSITORY&quot;,&quot;click_visual_representation&quot;:&quot;REPOSITORY_NAME_HEADING&quot;,&quot;actor_id&quot;:49521558,&quot;record_id&quot;:21700699,&quot;originating_url&quot;:&quot;https://github.com/topics/ios&quot;,&quot;user_id&quot;:49521558}}"
        data-hydro-click-hmac="c38ef14c5a72214b8e946bde857c36653301cb96a15a6b1108242526485221b8"
        data-ga-click="Explore, go to repository, location:explore feed" href="/vsouza/awesome-ios" class="text-bold">
        awesome-ios
      </a>
    </h1>

在两个锚元素之间我想获取具有此属性和值data-ga-click="Explore, go to repository, location:explore feed"的锚标记的href值

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping xpath


    【解决方案1】:

    要获得这样的特定链接,您必须在 xpath 中传递此 data-ga-click 属性以获得唯一结果。

    for heading in headings:
       links = heading.find_elements_by_xpath('.//a[@data-ga-click="Explore, go to repository, location:explore feed"]')
       for l in links:
            links_list.append(l.get_attribute('href'))
    

    或 Css 选择器。

    for heading in headings:
       links = heading.find_elements_by_css_selector('a[data-ga-click="Explore, go to repository, location:explore feed"]')
       for l in links:
            links_list.append(l.get_attribute('href'))
    

    【讨论】:

      【解决方案2】:

      您是否只想要标题内具有该值的 a 标记。您需要使用 .对于子元素并使用数据属性值。

      heading.find_elements_by_xpath('.//a[@data-ga-click="Explore, go to repository owner, location:explore feed"]')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-03
        • 1970-01-01
        • 2013-03-04
        • 2014-04-17
        • 2018-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多