python - 使用 xpath 从多个来源提取文本
问题描述
我为我的项目构建了一个刮板来从 3 个站点中提取文本。我想为 3 个站点使用一个蜘蛛。其中 2 个网站的内容采用以下结构:
<div id="site1">
<p> this is a test </p>
</div>
<div id="site2">
<p> this is a test </p>
</div>
一个有这个:
<div class="site3">
<p> <span> this is a test </span> </p>
</div>
我可以使用以下方法从 2 个站点中提取文本:
response.xpath('//div[@id="site1" or @id="site2" or @class="site3"]//p/text()').extract()
如何修改此代码以从 site3 中提取文本?
解决方案
response.xpath('//div[@id="site1" or @id="site2"]//p/text() | //div[@class="site3"]//p/span/text()').extract()
推荐阅读
- azure - 从 Azure Apim 的上下文中读取命名值
- ios - Swift:FCM 更改通知的声音
- jquery - 如何使用ajax将键值对发送到url
- javascript - 将 3 变为 1000,将 2 变为 100 - getFixedDecimalValue 函数
- matlab - 如何从 MATLAB 中的侦听器将数据写入文件?
- java - 我们如何在java8中的DoubleSummaryStatistics对象中自定义count、avg、sum、min和max的顺序
- stream - 基于 Dart/Flutter 中的过滤器更新流
- api - API网关可以直接访问数据库吗?
- angular - Angular 6 自定义输入组件
- selenium - safaridriver 无法通过 ssh 工作