我正在使用 beautiful soup 抓取网站数据。我想要以下的 anchor 值(我的名字是昵称)。但是我在谷歌中搜索了很多但找不到任何完美的解决方案来解决我的查询。
news_panel = soup.findAll('div', {'class': 'menuNewsPanel_MenuNews1'})
for news in news_panel:
temp = news.find('h2')
print temp
输出:
<h2 class="menuNewsHl2_MenuNews1"><a href="index.php?ref=MjBfMDFfMDhfMTRfMV84XzFfOTk2NDA=">My name is nick</a></h2>
但我想要这样的输出:My name is nick
最佳答案
只需获取 text
属性:
>>> soup = BeautifulSoup('''<h2 class="menuNewsHl2_MenuNews1"><a href="index.php?ref=MjBfMDFfMDhfMTRfMV84XzFfOTk2NDA=">My name is nick</a></h2>''')
>>> soup.text
u'My name is nick'
关于python - 使用 beautifulsoup 抓取 <h2> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20988487/