[英]Python beautiful soup removing extra text
<div class="friendBlockContent">
Bartdavy<br>
<span class="friendSmallText">
Online
</span>
</div>
是html,我試過了
for div in soup.findAll("div", class_="friendBlockContent", ):
print(div)
如果他在線上,這給了我,我只想知道這個名字,我該怎么辦?
div
有兩個文本節點,您可以使用.strings
進行訪問,並使用.stripped_strings
獲取干凈的數據。 然后用name
和online
字段解壓縮兩個節點。
In [50]: for div in soup.findAll("div", class_="friendBlockContent", ):
...: name, online = div.stripped_strings
...:
In [51]: name
Out[51]: 'Bartdavy'
In [52]: online
Out[52]: 'Online'
實現此目的的好方法:
for div in soup.findAll("div",class_="friendBlockContent", ):
print(div.contents[0])
如果可以確保結構與您發布的結構相似,則可以使用以下代碼:
for div in soup.findAll("div", class_="friendBlockContent", ):
print(div.contents[0].strip())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.