我试图通过使用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