如何在 python 中使用 beautifulsoup4 来抓取标签中的内容
2个回答
展开全部
源代码
from bs4 import BeautifulSoup
html_doc = '''
<div class="line-title">
<span class="title">
<b>
111
<span class="t-small c-green">
(222)
</span>
</b>
</span>
<span class="sechovershow jzbtn c-lined small marl10 act-ugc-edit act-ugc-edit-base1" style="margin-top:-5px">
<i class="fa fa-pencil"></i>
编辑
</span>
</div>
'''
soup = BeautifulSoup(html_doc, "html.parser")
# 初级版
didi = soup.b.next_element.strip()
invest = soup.b.span.next_element.strip()
# 进阶版
didi, invest = soup.b.stripped_strings
from bs4 import BeautifulSoup
html_doc = '''
<div class="line-title">
<span class="title">
<b>
111
<span class="t-small c-green">
(222)
</span>
</b>
</span>
<span class="sechovershow jzbtn c-lined small marl10 act-ugc-edit act-ugc-edit-base1" style="margin-top:-5px">
<i class="fa fa-pencil"></i>
编辑
</span>
</div>
'''
soup = BeautifulSoup(html_doc, "html.parser")
# 初级版
didi = soup.b.next_element.strip()
invest = soup.b.span.next_element.strip()
# 进阶版
didi, invest = soup.b.stripped_strings
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询