网页爬虫 - python爬虫beautifulsoup string抓取问题
伊谢尔伦
伊谢尔伦 2017-04-18 09:03:16
[Python讨论组]

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(4)
大家讲道理
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

天蓬老师

@洛克 的想法不錯,把不要的標籤淬出或是移除,再取字串:

>>> from bs4 import BeautifulSoup
>>> html = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
>>> soup = BeautifulSoup(html)
>>> a_tag = soup.a
>>> i_tag = soup.i.extract()
>>> a_tag.string
'I linked to '

或是像 @cloverstd 說的:

>>> from bs4 import BeautifulSoup
>>> html = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
>>> soup = BeautifulSoup(html)
>>> a_tag = soup.a
>>> list(a_tag.strings)
[u'I linked to ', u'example.com']
>>> list(a_tag.strings)[0]
'I linked to '
>>> a_tag.contents[0]
'I linked to '

總之方法很多,任意組合囉...


我回答過的問題: Python-QA

迷茫

试试pyquery

ringa_lee

把下面标签先取出来,用bs4的函数删掉好象是remove()。
再取上面就行了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号