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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值