访问标签内的属性

我的目标是访问链接列表(位于"event?id=85042")。


<tr class="gvrow"> == $0

  <td>...</td>

  <td>

    <a href="event?id=85042"

                                text

                              </a>

  </td>


  ...

这也会重复,所以我想获取位于 event?id=... 中的链接列表,我已经从这个问题Python Selenium - get href value尝试过这种方法,但它返回一个充满 None 的列表


primary_links = driver.find_elements_by_class_name('gvrow')

links = [elem.get_attribute('href') for elem in primary_links]

print(links)

输出:


[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

期望的输出:


['https://www.iccf.com/event?id=85042', 'https://www.iccf.com/event?id=79897', 'https://www.iccf.com/event?id=66745', ...]

那我该怎么办呢?

这是 html 的更好表示 

http://img.mukewang.com/64acec420001127e03950377.jpg

慕的地8271018
浏览 63回答 1
1回答

湖上湖

通过以下代码行,您将获得一个包含该<td>元素和任何其他具有以下元素的列表class=gvrow:driver.find_elements_by_class_name('gvrow')显然,<td>您返回的元素没有该href属性。除了您的方法之外,您还可以使用另一种方法来获取链接:a_elements&nbsp;=&nbsp;driver.find_elements_by_xpath("//tr[@class='gvrow']/td/a") links&nbsp;=&nbsp;[a_element.get_attribute('href')&nbsp;for&nbsp;a_element&nbsp;in&nbsp;a_elements]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python