xpath不能只选择一个html标记

2024-10-01 22:33:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个网站获得一些数据,但当我使用下面的代码时,它返回所有匹配的元素,我只想返回第一个匹配!我已经试过了,但没有回音!你知道吗

# -*- coding: utf-8 -*-
import scrapy
from gumtree.items import GumtreeItem



class FlatSpider(scrapy.Spider):
    name = "flat"
    allowed_domains = ["gumtree.com"]
    start_urls = (
        'https://www.gumtree.com/flats-for-sale',
    )

    def parse(self, response):
        item = GumtreeItem()
        item['title'] = response.xpath('//*[@class="listing-title"][1]/text()').extract()
        return item

如何使用xpath选择器只选择一个元素?你知道吗


Tags: 数据代码importcom元素title网站response
2条回答

严格地说应该是response.xpath('(//*[@class="listing-title"])[1]/text()'),但是如果您想要获取每个广告的标题(例如创建一个项目),您可能应该这样做:

for article in response.xpath('//article[@data-q]'):
     item = GumtreeItem()
     item['title'] = article.css('.listing-title::text').extract_first()
     yield item

这是因为第一个元素实际上是空的-只过滤掉非空值并使用extract_first()-对我有效:

$ scrapy shell "https://www.gumtree.com/flats-for-sale" -s USER_AGENT="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36"
In [1]: response.xpath('//*[@class="listing-title"][1]/text()[normalize-space(.)]').extract_first().strip()
Out[1]: u'REDUCED to sell! Stunning Hove sea view flat.'

相关问题 更多 >

    热门问题