xpath - 通过使用 XPath 创建的响应进行解析
问题描述
使用 Scrapy,我想从 HTML 格式良好的网站中提取一些数据。使用 XPath,我可以提取项目列表,但我无法使用 XPath 从列表中的元素中提取额外数据
所有 XPath 都已使用 XPather 进行了测试。我已经使用包含网页的本地文件测试了这个问题,同样的问题。
开始:
# Get the webpage
fetch("https://www.someurl.com")
# The following gives me the expected items from the HTML
products = response.xpath("//*[@id='product-list-146620']/div/div")
物品是这样的:
<div data-pageindex="1" data-guid="13157582" class="col ">
<div class="item item-card item-card--static">
<div class="item-card__inner">
<div class="item__image item__image--overlay">
<a href="/www.something.anywhere?ref_gr=9801" class="ratio_custom" style="padding-bottom:100%">
</a>
</div>
<div class="item__text-container">
<div class="item__name">
<a class="item__name-link" href="/c.aspx?ref_gr=9801">The text I want</a>
</div>
</div>
</div>
</div>
</div>
当使用以下 Xpath 提取“我想要的文本”时,我没有得到任何东西:
XPATH_PRODUCT_NAME = "/div/div/div/div/div[contains(@class,'item__name')]/a/text()"
products[0].xpath(XPATH_PRODUCT_NAME).extract()
输出为空,为什么?
解决方案
试试下面的代码。
XPATH_PRODUCT_NAME = ".//div[@class='item__name']/a[@class='item__name-link']/text()"
products[0].xpath(XPATH_PRODUCT_NAME).extract()
推荐阅读
- php - 在另一个输入值的 AJAX 发布后使用返回值更改输入值
- android - onActivityResult 已弃用,如何处理 android(Java) 的谷歌登录片段?
- android - 在cardview中实现OnClickListener获取具体数据
- dictionary - Kibana - 两个数据集的交集
- javascript - 无法实现 100% 的 Jest 线路覆盖率
- kotlin - 如何将带有 HTTP 状态和正文的 Java(Kotlin)的 AWS lamda 自定义响应传播到 HTTP API 网关
- excel - 查找名称所在的列表并返回最高值
- python - 我们如何在 discord.py 上看到 uptime bot?
- python - 访问最近在 AWS S3 中插入的数据
- javascript - 如何使用反应钩子和可观察的设计模式保持状态更新?