python - Python 3 BS4 - 从标签中提取数据(续)
问题描述
所以我有看起来像这样的 HTML 代码。
<li data-ng-repeat="sector in data.sectors"> <a target="_self" data-ng-href="/stocks/quotes/-382G/components/A" href="/stocks/quotes/-382G/components/A"><span>SIC-3826 Laboratory Analytical Instruments</span></a> </li>
我想提取跨度标签中的信息。不幸的是,当我使用以下代码时:
tags = soup.findAll("li",attrs={"data-ng-repeat":"sector in data.sectors"})
# tags = soup.find_all("a",attrs= {"target=","data-ng-href="})
# tags = soup.find_all("a")
for tag in tags:
print(tag.text)
结果是 [[sector.description]]。我要提取的是包括“SIC-3826 Laboratory Analytical Instruments”在内的信息
任何帮助将不胜感激。我尝试了各种替代方法,但我无法获得我想要的信息。
先感谢您!
解决方案
是的,您需要做的就是:
x = """<li data-ng-repeat="sector in data.sectors"> <a target="_self" data-ng-href="/stocks/quotes/-382G/components/A" href="/stocks/quotes/-382G/components/A"><span>SIC-3826 Laboratory Analytical Instruments</span></a> </li>"""
from bs4 import BeautifulSoup
print(BeautifulSoup(x, "lxml").text)
推荐阅读
- python - 如何检查图像中的文本是否可见 python
- php - 致命错误:使用 go daddy 服务器上的 composer 耗尽允许的 1610612736 字节内存大小(尝试分配 83886080 字节)
- typescript - 打字稿路由器道具对象
- flutter - “Null”类型不是“Future”类型的子类型
' 使用 Mocktail 测试模拟的 http 客户端时 - angular - 本地存储,无法读取未定义的属性“platformId”
- arrays - 其元素指向另一个指针数组的指针数组
- flutter - 如何让 Transform.translate 工作 Flutter
- php - 在使用“woe_order_export_started”挂钩时确定 WooCommerce 订单是否包含订单备注
- python - 我有这个 LDA 代码,当我运行它时,我不断收到一个难以跟踪的错误
- r - R 基础,管道,用 max() 总结