扫码关注官方订阅号
超链接如下:
国际足球巴西世界杯
现在,我想提取锚文本:国际足球巴西世界杯 现在的方法:
a = """国际足球巴西世界杯""" soup = BeautifulSoup("".join(a)) print soup.contents[0].string
结果输出None,我知道问题出在标签a中还包含其他的标签(span),从而导致出错,不知道怎么实现呢?
欢迎选择我的课程,让我们一起见证您的进步~~
from bs4 import BeautifulSoup html = '<a target="_blank" href="http://www.baidu.com"><span id="video_hl">国际足球</span>巴西世界杯</a>' soup = BeautifulSoup(html) print(soup.select('a')[0].text)
解决办法:
a = """<a target="_blank" href="http://www.baidu.com"><span id="video_hl">国际足球</span>巴西世界杯</a>""" soup = BeautifulSoup("".join(a)) print soup.text
注:要使用bs4版本
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
解决办法:
注:要使用bs4版本