python - 使用类(Scrapy)在锚标记内查找文本的 Xpath
问题描述
我正在尝试使用 Xpath 提取 Anchor Tag 的链接
网址
<a class="text size-1x-small font-accent color-brand all-caps"
href="http://time.com/section/business"
data-reactid="199">
Business
</a>
代码
item["category"] = str(
response.xpath(
'//a[@class="text size-1x-small font-accent color-brand all-caps"]/text()'
).extract()
)
和python函数
def parseSave(self, response):
item = NYtimesItem()
item["category"] = response.xpath(
'//a[@class="text size-1x-small font-accent color-brand all-caps"]/text()'
).extract()
yield item
请告诉我我做错了什么预期的输出将是锚标签的文本。例如商业
解决方案
/text()
旨在获取元素的内部文本。要提取 href 属性,请/@href
改用。
推荐阅读
- android - Android Studio 停止在 XML 中推荐
- python - 如何在 Django 中自动查询?
- firebase - Google Tasks Api - 错误:3 INVALID_ARGUMENT:请求包含无效参数
- python - 从熊猫数据框中只获取字母和一些标点符号?
- javascript - 如何在 next.js 中使用 package.json 中的代理?
- r - R:如何匹配给定范围内的多个变量
- postgresql - 为什么在 F# 中使用 `TransactionScope` 时 NpgsqlConnection 没有正确关闭或按时打开?
- string - 最长公共子序列(LCS)直觉
- verilog - 在verilog中写管道的两种方式,哪一种更好?
- android - 华为移动服务开发——存储服务不存在