簡體   English   中英

Xpath返回相同的結果

[英]Xpath returning same result

嘗試使用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(),但也無法正常工作。 它會退還所有卡名,但不會與我正確抓取的其他數據配對。 而不是為一個價格輸出一個卡名,依此類推,它將為我提供所有卡名以及第一張卡的價格,以此類推。

您需要相對的 XPath:

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

更新修復了您的XPath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM