尝试使用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
我已经尝试过了,它只是为每个值返回null。
@Timmy哦,您的XPath错误。查看最新答案。