问题
I am trying to select just the href inside a specific tr tag.
Here is my code:
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!"
Here is the output:
<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>
I want to just select the link inside of the href.
Edit:
Thank you alecxe for the correct solution.
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"])
回答1:
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.
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"])
where .a is equivalent to .find("a").
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"])
来源:https://stackoverflow.com/questions/38903941/beautiful-soup-find-href