我有以下(简化的)html代码:
<div class="container">
<h1>content</h1>
<br>
<br>
<div class="creator">
<a href="example.com">user</a>
</div>
<br>
<br>
"description"
</div>
以及如何获得说明?
答案 0 :(得分:0)
有许多方法可以选择"description"
:
import requests
from bs4 import BeautifulSoup
html_doc = '''<div class="container">
<h1>content</h1>
<br>
<br>
<div class="creator">
<a href="example.com">user</a>
</div>
<br>
<br>
"description"
</div>'''
soup = BeautifulSoup(html_doc, 'html.parser')
desc = soup.find('div', class_='container').contents[-1]
print(desc)
打印:
"description"
或者:
desc = soup.find('div', class_='container').find_all(text=True, recursive=False)[-1]
print(desc)
或者:
desc = soup.find('div', class_='container').find_all('br')[-1].find_next(text=True)
print(desc)