xpath - 当标签为@href 时,xpath text() 返回“None”
问题描述
我正在尝试提取 HTML 标记中包含的文本以构建 python defaultdict
。为此,我需要清除所有 xpath 和/或 HTML 数据并只获取文本,我可以使用 来完成/text()
,除非它是href。
我如何抓取物品:
for item in response.xpath(
"//*[self::h3 or self::p or self::strong or self::a[@href]]"):
如果我打印上面的内容而不进行提取尝试,它的外观如何:
<Selector xpath='//*[self::h3 or self::p or self::a[@href]]' data='<h3> Some text here ...'>
<Selector xpath='//*[self::h3 or self::p or self::a[@href]]' data='<a href="https://some.url.com...'>
我想提取“Some text here”和“https://some.url.com”
我如何尝试提取文本:
item = item.xpath("./text()").get()
print(item):
结果:
Some text here
None
“无”是我希望看到的:https://some.url.com
在尝试了网上建议的各种方法后,我无法让它工作。
解决方案
尝试使用这一行来提取文本或@href
:
item = item.xpath("./text() | ./@href").get()
推荐阅读
- qt - 如何在 IMX6Q 上为 Qt 启用 EGLFS i.Mx6 QPA?
- spring-boot - 出现异常时重启 Hazelcast Jet (v0.4)
- batch-file - 批量覆盖ftp服务器文件
- linux - 詹金斯奴隶问题 - 无效的流标头:099EACED
- sql-server - 我想在一个表中获取所有 SQL Server 登录详细信息
- javascript - d3:禁用“dblclick.zoom”默认行为在使用自定义缩放后返回
- r - ggplot2 - 在所有方面都包含一个因子级别
- r - 如何在R中的循环中使用tapply
- sql - 如何在过滤后订购有很多关系
- cocoa - 访问 NSBezierPath 的元素