我試圖通過使用beautifulsoup從html代碼中刪除br
標記。Python beautifulsoup刪除自我關閉標記
HTML如:
<span class="qualification" style="font-size:14px; font-family: Helvetica, sans-serif;">
Doctor of Philosophy (Software Engineering), Universiti Teknologi Petronas
<br>
Master of Science (Computer Science), Government College University Lahore
<br>
Master of Science (Computer Science), University of Agriculture Faisalabad
<br>
Bachelor of Science (Hons) (Agriculture),University of Agriculture Faisalabad
<br></span>
我的Python代碼:
for link2 in soup.find_all('br'):
link2.extract()
for link2 in soup.findAll('span',{'class':'qualification'}):
print(link2.string)
的問題是,以前的代碼只是獲取第一個資格。
所以,如果beautifulsoup自動添加了''
結束標記,可這個問題可以通過使用XHTML兼容''
避免? – HolyDanna
@HolyDanna:是的。儘管如此,OP仍然需要使用'Tag.text'或'Tag.stripped_strings'來獲取'span'的內容。 – vaultah