簡體   English   中英

Python:在之前之后提取文本<br/>

[英]Python:extract text after </span> before <br/>

這是我要處理的html文件:

<span class="pl">Countries:</span> USA <br/>
<span class="pl">Language:</span> English <br/>

這是我的python代碼:

from bs4 import BeautifulSoup

record=[]
soup=BeautifulSoup(html)
spans=soup.find_all('span')
for span in spans:
   record.append(span.text)

我終於得到的是:

Countries: Language:

結果錯過了一些重要信息:“美國”和“英語”如何獲取文本?

使用.next_sibling表示法:

soup.find("span", text="Countries:").next_sibling
soup.find("span", text="Language:").next_sibling

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM