Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scrapy程序不是在刮取所有数据_Python_Web Scraping_Scrapy_Scrapy Splash - Fatal编程技术网

Python Scrapy程序不是在刮取所有数据

Python Scrapy程序不是在刮取所有数据,python,web-scraping,scrapy,scrapy-splash,Python,Web Scraping,Scrapy,Scrapy Splash,我正在用scrapy编写一个程序来刮取下一页,它只是刮取第一行数据,而不是其余的。我认为这与for循环有关,但当我将循环更改为更宽时,它会输出太多数据,因为它会多次输出每行数据 def parse(self, response): item = GameItem() saved_name = "" for game in response.css("div.row.mt-1.list-view"): saved_name

我正在用scrapy编写一个程序来刮取下一页,它只是刮取第一行数据,而不是其余的。我认为这与for循环有关,但当我将循环更改为更宽时,它会输出太多数据,因为它会多次输出每行数据

 def parse(self, response):
        item = GameItem()
        saved_name = ""
        for game in response.css("div.row.mt-1.list-view"):
            saved_name  = game.css("a.card-text::text").get() or saved_name
            item["Card_Name"] = saved_name.strip()
            if item["Card_Name"] != None:
                saved_name = item["Card_Name"].strip()
            else:
                item["Card_Name"] = saved_name
            yield item
更新#1



    def parse(self, response):
        for game in response.css('div.card > div.row'):
            item = GameItem()
            item["Card_Name"]  = game.css("a.card-text::text").get()
            for buying_option in game.css('div.buying-options-table div.row:not(:first-child)'):
                item["Condition"] = game.css("div.col-3.text-center.p-1::text").get()
                item["Price"] = game.css("div.col-2.text-center.p-1::text").get()
            yield item

response.css(“div.row.mt-1.list view”)只返回一个选择器,因此循环中的代码只运行一次。尝试以下操作:
for game in response.css(“.mt-1.list-view.card text”):
您将得到一个选择器列表以循环使用。

response.css(“div.row.mt-1.list view”)
只返回一个选择器,因此循环中的代码只运行一次。试试这个:
用于游戏响应.css(“.mt-1.list-view.card text”):
你会得到一个选择器列表来循环。

我想你需要下面的css(以后你可以使用它作为处理
购买选项的基础):


如您所见,我在循环中移动了
item=GameItem()
。在
保存的游戏中也不需要
在这里。

我想你需要下面的CSS(以后你可以使用它作为处理
购买选项的基础
容器):

如您所见,我在循环中移动了
item=GameItem()
。另外,这里不需要保存游戏。

您的代码无效,因为您正在列表循环之外创建GameItem()。我一定错过了一张关于.get()和.getall()方法的明信片。也许有人能评论一下它和摘录有什么不同

您失败的代码

 def parse(self, response):
        item = GameItem() # this line right here only creates 1 game item per page
        saved_name = ""
        for game in response.css("div.row.mt-1.list-view"): # this line fails since it gets all the items on the page. This is a wrapper wrapping all the items inside of it. See below code for corrected selector.
            saved_name  = game.css("a.card-text::text").get() or saved_name
            item["Card_Name"] = saved_name.strip()
            if item["Card_Name"] != None:
                saved_name = item["Card_Name"].strip()
            else:
                item["Card_Name"] = saved_name
            yield item
解决问题的固定代码:

 def parse(self, response):
        for game in response.css("div.product-col"):
            item = GameItem()
            item["Card_Name"] = game.css("a.card-text::text").get()
            if not item["Card_Name"]:
                continue # this will skip to the next item if there is no card name, if there is a card name it will continue to yield the item. Another way of doing this would be to return nothing. Just "return". You only do this if you DO NOT want code after executed. If you want the code after to execute then use yeid.
            yield item
您的代码无效,因为您正在列表循环之外创建GameItem()。我一定错过了一张关于.get()和.getall()方法的明信片。也许有人能评论一下它和摘录有什么不同

您失败的代码

 def parse(self, response):
        item = GameItem() # this line right here only creates 1 game item per page
        saved_name = ""
        for game in response.css("div.row.mt-1.list-view"): # this line fails since it gets all the items on the page. This is a wrapper wrapping all the items inside of it. See below code for corrected selector.
            saved_name  = game.css("a.card-text::text").get() or saved_name
            item["Card_Name"] = saved_name.strip()
            if item["Card_Name"] != None:
                saved_name = item["Card_Name"].strip()
            else:
                item["Card_Name"] = saved_name
            yield item
解决问题的固定代码:

 def parse(self, response):
        for game in response.css("div.product-col"):
            item = GameItem()
            item["Card_Name"] = game.css("a.card-text::text").get()
            if not item["Card_Name"]:
                continue # this will skip to the next item if there is no card name, if there is a card name it will continue to yield the item. Another way of doing this would be to return nothing. Just "return". You only do this if you DO NOT want code after executed. If you want the code after to execute then use yeid.
            yield item

你没有澄清你的预期产出。你没有澄清你的预期产出。它不再给我不必要的数据,只是给我我需要的卡片名称。但定价数据的循环不起作用。即使有多个卖家,每个都有不同的价格,特定的卡只输出一次,而不是4个或5个,或者无论有多少不同的卖家,它都不会给我任何价格。相反,它从标题行获取数据,只输出每个价格的“数量”。我已经用新的代码和当前输出的部分快照更新了我的答案。我现在看到,在我急于尝试让程序工作的时候,我忘记了在第二个循环中将游戏更改为“购买”选项。现在程序运行良好。谢谢你的帮助!好吧,它不再给我不必要的数据,它只是给我我需要的卡名。但定价数据的循环不起作用。即使有多个卖家,每个都有不同的价格,特定的卡只输出一次,而不是4个或5个,或者无论有多少不同的卖家,它都不会给我任何价格。相反,它从标题行获取数据,只输出每个价格的“数量”。我已经用新的代码和当前输出的部分快照更新了我的答案。我现在看到,在我急于尝试让程序工作的时候,我忘记了在第二个循环中将游戏更改为“购买”选项。现在程序运行良好。谢谢你的帮助!