网页爬虫 - python爬虫beautifulsoup string抓取问题

查看:158
本文介绍了网页爬虫 - python爬虫beautifulsoup string抓取问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我要的是这个蓝色部分的内容,但是beautifulsoup里两个方法,一个.strings还有一个get_text()都不行,他们会把下面span里的string:Good Sister-in-lwa:Forbidden love这些都抓取。.string直接抓不到,因为这个方法无法判断该抓取哪个string。
所以我该怎么解决标签里内嵌标签的抓取字符串问题

解决方案

In [1]: from bs4 import BeautifulSoup

In [2]: html_doc = "<a>123<span>321</span></a>"

In [3]: soup = BeautifulSoup(html_doc, 'html.parser')

In [4]: soup.a.contents[0]
Out[4]: u'123'

In [5]: soup.a.contents
Out[5]: [u'123', <span>321</span>]

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#contents-and-children

这篇关于网页爬虫 - python爬虫beautifulsoup string抓取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆