首页 > 解决方案 > 使用 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 中提取文本?

标签: pythonhtmlxpathweb-scrapingscrapy

解决方案


response.xpath('//div[@id="site1" or @id="site2"]//p/text() | //div[@class="site3"]//p/span/text()').extract() 

推荐阅读