在Python中使用BeautifulSoup解析HTML

3 投票
1 回答
7155 浏览
提问于 2025-04-16 19:49

我写了一些代码来解析HTML,但结果并不是我想要的:

import urllib2
html = urllib2.urlopen('http://dummy').read()
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html)
for definition in soup.findAll('span', {"class":'d'}):
definition = definition.renderContents()
print "<meaning>", definition
for exampleofuse in soup.find('span',{"class":'x'}):
    print "<exampleofuse>", exampleofuse, "<exampleofuse>"
print "<meaning>"

有没有什么方法可以在类属性是"d"或"x"的时候获取字符串呢?

下面是我想要解析的HTML代码:

<span class="d">calculated by adding several amounts together</span>
<span class="x">an average rate</span>
<span class="x">at an average speed of 100 km/h</span>
<span class="d">typical or normal</span>
<span class="x">average intelligence</span>
<span class="x">20 pounds for dinner is average</span>

然后,这就是我想要的结果:

<definition>calculated by adding several amounts together
    <example_of_use>an average rate</example_of_use>
    <example_of_use>at an average speed of 100 km/h</example_of_use>
</definition>
<definition>typical or normal
    <example_of_use>average intelligence</example_of_use>
    <example_of_use>20 pounds for dinner is average</example_of_use>
</definition>

1 个回答

5

是的,你可以获取网页中的所有标签,然后逐个检查它们是否有“d”或“x”这个类。如果有的话,就把它们打印出来。

下面这个代码可能可以实现这个功能(未经测试):

for span in soup.findAll('span'):
    if span.find("span","d").string:
        print "<definition>" + span.find("span","d").string + "</definition>"
    elif span.find("span","x").string:
        print "<example>" + span.find("span","x").string + "</example>"

撰写回答