Я написал некоторый код для разбора 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>