温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Xpath returning same result
python scrapy xpath

python - Xpath返回相同的结果

发布于 2020-03-27 11:03:54

尝试使用Xpath从以下网站上刮取卡名:https: //www2.trollandtoad.com/buylist/?_ga=2.123753418.115346513.1562026676-1813285172.1559913561#!/M/10591 ,但每次都始终返回相同的结果。我需要它来输出该链接中的所有卡名,但是它一次又一次地给了我相同的卡名。

def parse(self, response):
        #  Initialize item to function GameItem located in items.py, will be called multiple times
        item = GameItem()
        # Extract card category from URL using html code from website that identifies the category.  Will be outputted before rest of data
        for data in response.css('tr.ng-scope'):
            item["Set"] =data.css("a.ng-binding.ng-scope::text").get()
            if item["Set"] == None:
                item["Set"] = data.css("span.ng-binding.ng-scope::text").get()
            item["Card_Name"]  = data.xpath("//div/table/tbody/tr/td[contains(@class,'buylist_productname item')]/a/text()").get()

我尝试使用getall(),但也无法正常工作。它会退还所有卡名,但不会与我正确抓取的其他数据配对。而不是以一个价格输出一个卡名,依此类推,它将为我提供所有卡名以及第一张卡的价格,以此类推。

查看更多

查看更多

提问者
Tom
被浏览
123
gangabass 2019-07-03 22:21

您需要相对的 XPath:

item["Card_Name"]  = data.xpath(".//td[2]/a/text()").get()

更新修复了您的XPath