我是靠谱客的博主 火星上冷风,这篇文章主要介绍beautifulsoup去除标签_python-从使用BeautifulSoup解析的HTML中删除标签,现在分享给大家,希望可以做个参考。

我是python的新手,正在使用BeautifulSoup解析网站,然后提取数据.我有以下代码:

for line in raw_data: #raw_data is the parsed html separated into smaller blocks

d = {}

d['name'] = line.find('div', {'class':'torrentname'}).find('a')

print d['name']

Ubuntu 9.10 desktop (i386)

通常,我可以通过编写以下内容来提取“ Ubuntu 9.10桌面(i386)”:

d['name'] = line.find('div', {'class':'torrentname'}).find('a').string

但是由于强大的html标签,它返回None.有没有办法提取强标签然后使用.string还是有更好的办法?我尝试使用BeautifulSoup的extract()函数,但是无法正常工作.

编辑:我只是意识到,如果有两组强标签,因为单词之间的间隔被忽略了,我的解决方案将无法工作.解决该问题的方法是什么?

解决方法:

使用“ .text”属性:

d['name'] = line.find('div', {'class':'torrentname'}).find('a').text

或者在findAll(text = True)上加入:

anchor = line.find('div', {'class':'torrentname'}).find('a')

d['name'] = ''.join(anchor.findAll(text=True))

标签:beautifulsoup,html,python,parsing

来源: https://codeday.me/bug/20191209/2096973.html

最后

以上就是火星上冷风最近收集整理的关于beautifulsoup去除标签_python-从使用BeautifulSoup解析的HTML中删除标签的全部内容,更多相关beautifulsoup去除标签_python-从使用BeautifulSoup解析内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部