使用 beautifulsoup python 从标签中提取 html 数据
Extract html data from tags using beautifulsoup python
我已将 objective 字符串缩小为以下 html:
<h2 class="user-name"> John Kennity <span class="top-class"><a href="http://service-web.com/2008-07-31/11" target="_blank">highest rank </a></span>
</h2>
我想访问 John Kennith 这个名字。我怎样才能在 beautifulsoup python 中做到这一点?
希望对您有所帮助。
user_names = soup.findAll('div', {'class': 'user-name'})
for un in user_names:
temp = un.find('h2')
if temp:
print temp.text
import bs4
text = '''<h2 class="user-name"> John Kennity <span class="top-class"><a href="http://service-web.com/2008-07-31/11" target="_blank">highest rank </a></span>
</h2>'''
soup = bs4.BeautifulSoup(text, 'lxml')
name, rank = soup.h2.stripped_strings
输出:
'John Kennity'
我已将 objective 字符串缩小为以下 html:
<h2 class="user-name"> John Kennity <span class="top-class"><a href="http://service-web.com/2008-07-31/11" target="_blank">highest rank </a></span>
</h2>
我想访问 John Kennith 这个名字。我怎样才能在 beautifulsoup python 中做到这一点?
希望对您有所帮助。
user_names = soup.findAll('div', {'class': 'user-name'})
for un in user_names:
temp = un.find('h2')
if temp:
print temp.text
import bs4
text = '''<h2 class="user-name"> John Kennity <span class="top-class"><a href="http://service-web.com/2008-07-31/11" target="_blank">highest rank </a></span>
</h2>'''
soup = bs4.BeautifulSoup(text, 'lxml')
name, rank = soup.h2.stripped_strings
输出:
'John Kennity'