python - 使用 beautifulsoup 抓取 <h2> 标签

标签 python web-scraping beautifulsoup

我正在使用 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/

相关文章:

Python套接字编程和ISO-OSI模型

python - Scipy convolve2d 具有像 Theano 的 conv2d 那样的子采样功能吗?

python - Django 中的显示表单不基于模型/表单

html - R - 如何从 XML 节点集中提取项目?

python - 使用 BeautifulSoup 在 Python 中提取嵌入式 <span>

python-3.x - Python 3 BeautifulSoup4 在源页面中搜索文本

python - 翼IDE + Python 2.4 : no module named functools

javascript - 查找链接所在位置

Python Selenium 从 url 中抓取丢失的图像

python - 如何使用 python/Beautiful Soup 从维基百科表中提取特定列