xpath relative path不返回

时间:2017-03-27 12:35:13

标签: xpath scrapy

当我放置一个点来使我的xpath相对时,它返回{" name":" none"},当我删除点时,它会重复相同的数据直到循环端

.//*[@id="listingAds"]/section/section/ul/li[1]/a/section/h2

这是完整的代码

import scrapy
class BrickSetSpider(scrapy.Spider):
    name = "brickset_spider"
    start_urls = ['https://www.leboncoin.fr/annonces/offres/bretagne/']

    def parse(self, response):
        SET_SELECTOR = '.dontSwitch .trackable'
        for annonce in response.css(SET_SELECTOR):


            NAME_SELECTOR = './/*[@id="listingAds"]/section/section/ul/li[1]/a/section/h2'
            NAME_CATEGORIE = '.item_title+ .item_supp ::text'
            NAME_PLACE = '.item_supp+ .item_supp ::text'
            NAME_PRICE = '.item_price ::text'

            yield {
            'name': annonce.xpath(NAME_SELECTOR).extract_first(),
            }

1 个答案:

答案 0 :(得分:0)

那是因为您的SET_SELECTOR已经将您带入了列表清单。换句话说,每个annonce选择器对应于每个搜索结果元素(每个列表)。适当调整NAME_SELECTOR

NAME_SELECTOR = './/h2/text()'