美丽的汤找到href [英] Beautiful soup find href
问题描述
我正在尝试仅选择特定 tr 标签内的 href.
I am trying to select just the href inside a specific tr tag.
这是我的代码:
soup=bs(driver.page_source, 'html.parser')
obj=soup.find(text="test545")
new=obj.parent.previous_sibling.previous_sibling.previous_sibling
print new
if new.has_key('href'):
new=new['href']
print"found!"
输出如下:
<td headers="LINK"><a href="f?p=106:3:92877880706::NO::P3_ID:5502&cs=tmX92fFLmToJQ69ZOs2w"><img border="0" src="/i_5.0/menu/pencil3416x16.gif"/></a></td>
我只想选择 href 内的链接.
I want to just select the link inside of the href.
感谢 alecxe 提供正确的解决方案.
Thank you alecxe for the correct solution.
解决方案-
soup=bs(driver.page_source, 'html.parser')
obj=soup.find(text="test545")
td = obj.find_previous("td", headers="LINK")
link = td.a
print(link["href"])
推荐答案
提供页面的完整 HTML,包括带有 text="test545" 的元素的相对位置和所需链接,将有助于为您提供更多强大的解决方案.
Providing a full HTML of the page including the relative location of the element with text="test545" and the desired link, would help to provide you with a more robust solution.
但是,鉴于问题中发布的内容,您可以在 new
元素中找到 a
元素:
But, given what was posted in the question, you can just find the a
element inside the new
element:
link = new.a
print(link["href"])
其中 .a
等价于 .find("a")
.
还可以尝试使用 .find_previous()
方法从 obj
元素中定位 td
元素:
Also try using the .find_previous()
method to locate the td
element from the obj
element:
obj = soup.find(text="test545")
td = obj.find_previous("td", headers="LINK")
link = td.a
print(link["href"])
这篇关于美丽的汤找到href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!