Python Selenium如何仅在锚标记包含某些属性值的情况下获取锚标记href值

安东尼·戴维

我想从GitHub搜索结果中获取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>

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

昆杜克

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

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'))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

python matplotlib:如何在仅知道x值的情况下向曲线添加点标记?

如何获取 Python Selenium 开始和结束标记之间的值?

如何在没有href =“#”的情况下将焦点放在锚标记上

无法使用jQuery获取锚标记的href值

JavaScript中的多个锚标记href值

如何使用golang从锚标记的href属性中的变量连接值

如何从字符串中使用javascript获取锚标记的href值

单击锚标记时如何在服务器端获取href值

无法使用 jquery 获取锚标记值

如何获取锚标记的Name属性

如何在jQuery中获取锚标记URL哈希值?

如何获得锚标记中的值?

Python Selenium - 获取 href 值

在这种情况下如何使用 python/selenium 将返回值传递给函数?

如何使用 Selenium 获取属性值 - Python

如何从锚标记获取文本?

导航到URL并从锚标记中获取href属性?

在 python regex 模块中查找锚标记的问题

如何在python中给定值的情况下获取标签名称?

Python:如何在不循环的情况下获取给定值的dict键?

HTML:如何在不影响视觉显示的情况下添加锚标记?

在glyphicon和锚标记中都包含href

如何使锚标记的 href 依赖于类属性?

如何使用 Python+Selenium 从给定的 HTML 中获取 href 属性值?

Python Beautifulsoup,在标记中获取href标记

Python Beautifulsoup:如何在不知道相应属性名称的情况下通过属性值查找标签?

如何从类中获取href值-Python-Selenium

如何通过锚标记帮助器传递值数组?

如何在锚标记内输出项目值