Python beautifulsoup删除自动关闭标记

2024-04-16 15:23:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用beautifulsoup从html代码中删除br标记。在

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代码:

^{pr2}$

问题是之前的代码只获得第一个限定。在


Tags: of代码标记brmasterhtmlcomputerclass
3条回答

因为这些<br>都没有关闭的对应项,Beautiful Soup会自动添加它们,从而生成以下HTML:

In [23]: soup = BeautifulSoup(html)

In [24]: soup.br
Out[24]: 
<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/></br></br></br>

当您在第一个<br>标记上调用Tag.extract时,您将删除其所有子代及其子代包含的字符串:

^{pr2}$

似乎您只需从span元素提取所有文本。如果是这样的话,不要费心移除任何东西:

In [28]: soup.span.text
Out[28]: '\nDoctor of Philosophy ( Software Engineering ), Universiti Teknologi Petronas\n\nMaster of Science (Computer Science), Government College University Lahore\n\nMaster of Science ( Computer Science ), University of Agriculture Faisalabad\n\nBachelor of Science (Hons) ( Agriculture ),University of Agriculture Faisalabad\n'

Tag.text属性从给定标记中提取所有字符串。在

使用“展开”应该可以

soup = BeautifulSoup(html)
for match in soup.findAll('br'):
    match.unwrap()

以下是一种方法:

for link2 in soup.findAll('span',{'class':'qualification'}):
    for s in link2.stripped_strings:
        print(s)

没有必要删除<br>标记,除非您需要删除它们以便以后处理。这里的link2.stripped_strings是一个生成器,它生成标记中的每个字符串,去掉前导空格和尾随空格。打印循环可以更简洁地写为:

^{pr2}$

相关问题 更多 >