Python 如何在使用scrapy的元素中选择特定元素

Python 如何在使用scrapy的元素中选择特定元素,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,这就是我到目前为止所做的——它不太管用。它会刮取url而不是h2标记。当它位于这么多div中时,我该怎么做?为了在scrapy中解析元素,您需要以“.”开头xpath。否则,您将通过响应进行解析,这是正确的方法 import scrapy class rlgSpider(scrapy.Spider): name = 'bot' start_urls = [ 'https://rocket-league.com/trading?filterItem=0&filt

这就是我到目前为止所做的——它不太管用。它会刮取url而不是h2标记。当它位于这么多div中时,我该怎么做?

为了在scrapy中解析元素,您需要以“.”开头xpath。否则,您将通过响应进行解析,这是正确的方法

import scrapy

class rlgSpider(scrapy.Spider):
    name = 'bot'

    start_urls = [
    'https://rocket-league.com/trading?filterItem=0&filterCertification=0&filterPaint=0&filterPlatform=1&filterSearchType=1&filterItemType=0&p=1']

    def parse(self, response):
        data = {}
        offers = response.xpath('//div[@class = "col-3-3"]')
        for offer in offers:
            for item in offer.xpath('//div[@class = "rlg-trade-display-container is--user"]/div[@class = "rlg-trade-display-items"]/div[@class = "col-1-2 rlg-trade-display-items-container"]/a'):
                data['name'] = item.xpath('//div/div[@position ="relative"]/h2').extarct()
                yield data
extarct()???这在您的代码中吗?请参阅。
def parse(self, response):

    offers = response.xpath('//div[@class = "col-3-3"]')
    for offer in offers:
        for item in offer.xpath('.//div[@class = "rlg-trade-display-container is--user"]/div[@class = "rlg-trade-display-items"]/div[@class = "col-1-2 rlg-trade-display-items-container"]/a'):
            data = {}
            data['name'] = item.xpath('.//h2/text()').extarct_first()
            yield data