我的目标是访问链接列表(位于"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 的更好表示
1 回答
![?](http://img1.sycdn.imooc.com/5458463b0001358f02200220-100-100.jpg)
湖上湖
TA贡献2003条经验 获得超2个赞
通过以下代码行,您将获得一个包含该<td>
元素和任何其他具有以下元素的列表class=gvrow
:
driver.find_elements_by_class_name('gvrow')
显然,<td>
您返回的元素没有该href
属性。
除了您的方法之外,您还可以使用另一种方法来获取链接:
a_elements = driver.find_elements_by_xpath("//tr[@class='gvrow']/td/a") links = [a_element.get_attribute('href') for a_element in a_elements]
添加回答
举报
0/150
提交
取消