1 回答
TA贡献1790条经验 获得超9个赞
对于调用的内容,有一个非常有用的方法,它返回一个包含标记的子级的列表:
from bs4 import BeautifulSoup
html = '''<div id='b_detalii_caracteristici'>
<ul class="lista-tabelara">
<li>
"Nr. camere:"
<span>2</span>
</li>
<li>
"Suprafata utila:"
<span>44mp</span>
</li>
</ul>
</div>'''
soup = BeautifulSoup(html, 'html.parser')
lis = soup.select('#b_detalii_caracteristici ul.lista-tabelara li')
for li in lis:
li_content = li.contents
li_text = li_content[0].strip()
span_text = li_content[1].text
print('li_content ==> ',li_content)
print('li_text ==> ',li_text)
print('span_text ==>',span_text)
输出:
li_content ==> ['\n "Nr. camere:"\n ', <span>2</span>, '\n']
li_text ==> "Nr. camere:"
span_text ==> 2
li_content ==> ['\n "Suprafata utila:"\n ', <span>44mp</span>, '\n']
li_text ==> "Suprafata utila:"
span_text ==> 44mp
添加回答
举报