html代碼是:
<div class="card border p-3">
<span class="small text-muted">Contact<br></span>
<div>Steven Cantrell</div>
<div class="small">Department of Justice</div>
<div class="small">Federal Bureau of Investigation</div>
<!---->
<!---->
<!---->
<div class="small"><a href="mailto:[email protected] ">[email protected]</a></div>
<div class="small">256-313-8835</div>
</div>
我想在<div>標簽 ie 中獲取輸出Steven Cantrell 。
我需要這樣一種方式,我應該能夠獲得下一個標簽的內容。在這種情況下,它是'span',{'class':'small text-muted'}
我試過的是:
rfq_name = soup.find('span',{'class':'small text-muted'})
print(rfq_name.next)
但這是列印Contact而不是名稱。
uj5u.com熱心網友回復:
您快到了,只需將您的列印更改為: print(rfq_name.find_next('div').text)
找到包含文本的元素"Contact"。然后用于.find_next()獲取下一個<div>標簽。
from bs4 import BeautifulSoup
html = '''<div >
<span >Contact<br></span>
<div>Steven Cantrell</div>
<div >Department of Justice</div>
<div >Federal Bureau of Investigation</div>
<!---->
<!---->
<!---->
<div ><a href="mailto:[email protected] ">[email protected]</a></div>
<div >256-313-8835</div>
</div>'''
soup = BeautifulSoup(html, 'html.parser')
contact = soup.find(text='Contact').find_next('div').text
輸出:
print(contact)
Steven Cantrell
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/329052.html
