我写了一些代码来解析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 "", definition
for exampleofuse in soup.find('span',{"class":'x'}):
print "", exampleofuse, ""
print ""
当class属性为“d”或“x”然后获取字符串时,有什么方法吗?
以下html代码是我要解析的:
calculated by adding several amounts together
an average rate
at an average speed of 100 km/h
typical or normal
average intelligence
20 pounds for dinner is average
然后,这是我想要的结果:
calculated by adding several amounts together
an average rate
at an average speed of 100 km/h
typical or normal
average intelligence
20 pounds for dinner is average