0
我想只選擇一個特定的tr標籤內的href。美麗的湯找到href
這裏是我的代碼:
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裏面的鏈接。
編輯:
謝謝alecxe的正確解決方案。
解決方案 -
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"])
你的代碼發佈。它不工作?它拋出錯誤嗎?什麼錯誤?請添加更多詳細信息。 – JeffC
對不起傑夫,我認爲從輸出中可以明顯看出。沒有錯誤。問題是我只想獲得href值。在我的代碼中,我認爲「新」應該有一個href鍵,但它沒有。 Alecxe解決了我的問題。 –