Beautiful soup find href

倖福魔咒の 提交于 2019-12-25 04:46:09

问题


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&amp;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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!