【问题标题】:extract text with xpath from multiple sources使用 xpath 从多个来源提取文本
【发布时间】:2020-03-08 22:59:20
【问题描述】:

我构建了一个抓取工具来为我的项目从 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 中提取文本?

【问题讨论】:

标签: python html xpath web-scraping scrapy


【解决方案1】:
response.xpath('//div[@id="site1" or @id="site2"]//p/text() | //div[@class="site3"]//p/span/text()').extract() 

【讨论】:

    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多