Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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 基于https://github.com/scrapy/quotesbot/blob/master/quotesbot/spiders/toscrape-xpath.py 不使用屈服请求传递数据_Python_Web Scraping_Scrapy_Meta - Fatal编程技术网

Python 基于https://github.com/scrapy/quotesbot/blob/master/quotesbot/spiders/toscrape-xpath.py 不使用屈服请求传递数据

Python 基于https://github.com/scrapy/quotesbot/blob/master/quotesbot/spiders/toscrape-xpath.py 不使用屈服请求传递数据,python,web-scraping,scrapy,meta,Python,Web Scraping,Scrapy,Meta,strong text我基于搜索的示例编写的代码似乎没有达到预期效果,因此我决定使用github上的工作模型: 然后我稍微修改了它,以展示我遇到的情况。下面的代码按预期工作得很好,但我的最终目标是将刮取的数据从第一个“parse”传递到第二个“parse2”函数,以便我可以组合来自两个不同页面的数据。但现在我想从一个非常简单的开始,这样我就可以了解正在发生的事情,因此下面的代码被严重剥离 # -*- coding: utf-8 -*- import scrapy from quotesbot.i

strong text我基于搜索的示例编写的代码似乎没有达到预期效果,因此我决定使用github上的工作模型: 然后我稍微修改了它,以展示我遇到的情况。下面的代码按预期工作得很好,但我的最终目标是将刮取的数据从第一个“parse”传递到第二个“parse2”函数,以便我可以组合来自两个不同页面的数据。但现在我想从一个非常简单的开始,这样我就可以了解正在发生的事情,因此下面的代码被严重剥离

# -*- coding: utf-8 -*-
import scrapy
from quotesbot.items import MyItems
from scrapy import Request


class ToScrapeSpiderXPath(scrapy.Spider):
    name = 'toscrape-xpath'
    start_urls = [
    'http://quotes.toscrape.com/',
    ]

def parse(self, response):
    item = MyItems()
    for quote in response.xpath('//div[@class="quote"]'):
            item['tinfo'] = 
quote.xpath('./span[@class="text"]/text()').extract_first()
            yield item 



but then when I modify the code as below:

# -*- coding: utf-8 -*-
import scrapy
from quotesbot.items import MyItems
from scrapy import Request


class ToScrapeSpiderXPath(scrapy.Spider):
    name = 'toscrape-xpath'
    start_urls = [
        'http://quotes.toscrape.com/',
    ]

def parse(self, response):
    item = MyItems()
    for quote in response.xpath('//div[@class="quote"]'):
            item['tinfo'] =  
            quote.xpath('./span[@class="text"]/text()').extract_first()
            yield Request("http://quotes.toscrape.com/", 
    callback=self.parse2, meta={'item':item})

def parse2(self, response):
    item = response.meta['item']
    yield item
我只刮了一件,上面说其余的都是复制品。看起来“parse2”根本就没有被读取。我一直在玩缩进和括号,以为我错过了一些简单的东西,但没有太大的成功。我看了很多例子,看看我是否能理解问题所在,但我仍然无法让它起作用。我相信这对于那些大师来说是一个非常简单的问题,所以我大叫“救命!”有人

另外,我的items.py文件如下所示,我认为这两个文件items.py和toscrape-xpath.py是我所知道的唯一有效的文件,因为我对这一切都是新手

# -*- coding: utf-8 -*-`enter code here`

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class QuotesbotItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass

class MyItems(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    tinfo = scrapy.Field()
    pass
非常感谢您提供的一切帮助

# -*- coding: utf-8 -*-
import scrapy
from quotesbot.items import MyItems
from scrapy import Request


class ToScrapeSpiderXPath(scrapy.Spider):
    name = 'toscrape-xpath'
    start_urls = [
    'http://quotes.toscrape.com/',
    ]

def parse(self, response):
    item = MyItems()
    for quote in response.xpath('//div[@class="quote"]'):
            item = 
{'tinfo':quote.xpath('./span[@class="text"]/text()').extract_first()}
    **yield response.follow**('http://quotes.toscrape.com', self.parse_2, 
meta={'item':item})

def parse_2(self, response):
    print "almost there"
    item = response.meta['item']
    yield item

您的爬行器逻辑非常混乱:

def parse(self, response):
    for quote in response.xpath('//div[@class="quote"]'):
            yield Request("http://quotes.toscrape.com/", 
    callback=self.parse2, meta={'item':item})
对于您在
quotes.toscrape.com
上找到的每一个报价,您是否安排了另一个对同一网页的请求? 发生的情况是,这些新的计划请求被scrapys duplicate request filter过滤掉

也许你应该把物品放在那里:

def parse(self, response):
    for quote in response.xpath('//div[@class="quote"]'):
        item = MyItems()
        item['tinfo'] = quote.xpath('./span[@class="text"]/text()').extract_first()
        yield item
要说明当前爬虫程序不执行任何操作的原因,请参见此图:

@jayuya你能详细说明你想做什么吗?第一个请求转到quotes.toscrape.com;第二个请求会去哪里?它不能再去quotes.toscrape.com了,至少这样做没有意义。好吧,我的朋友,让我换一种说法,为什么parse2函数中的代码没有“屈服”?我需要它来生成parse2函数中的项,这样我就可以在一个非常简单的示例中了解meta或将数据从一个解析函数传递到另一个解析函数。感谢Granitosaurus的帮助,我想我需要对我在这里尝试做的事情做更多的解释:我的代码的第一部分实际上与您的相同,我所做的复制/粘贴有点像是将两个部分的代码组合在一起,最终目标是像这样做,它们展示了如何“向回调函数传递额外的数据”。我所做的只是拿一个工作示例并修改它,这样我就可以学习如何使用meta传递数据,这似乎对我来说不起作用me@jayuya你还不明白这里的问题。请参阅我对答案的编辑。你的爬虫程序根本没有意义-你在
解析中发出的请求必须去同一个url以外的其他地方。很好的例子,现在,让我问一下:我如何获得与你的代码相同的结果,而不是使用像我这样的伪代码,它有第二个解析函数和元数据?我在这里看到了类似的问题:,另一个例子是,但我不太明白答案