基于https://github.com/scrapy/quotesbot/blob/master/quotesbot/spiders/toscrapexpath.py不使用屈服要求传递数据

2024-04-24 11:03:23 发布

您现在位置:Python中文网/ 问答频道 /正文

strong text基于我搜索的示例的代码似乎没有按预期运行,因此我决定使用在github上找到的工作模型:https://github.com/scrapy/quotesbot/blob/master/quotesbot/spiders/toscrape-xpath.py 然后我稍微修改了一下,以展示我遇到了什么。下面的代码工作得很好,但我的最终目标是将第一个“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”根本就没有读过。我一直在玩缩进和括号,以为我错过了一些简单的东西,但没有太大的成功。我看了很多例子,看看我是否能理解什么可能是问题,但我仍然无法使它工作。我相信这对那些大师来说是一个非常简单的问题,所以我大叫“救命!”某人!你知道吗

也是我的项目.py文件如下所示,我认为这两个文件项目.py还有托斯卡普-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

Tags: textfromimportselfcomforparseresponse
1条回答
网友
1楼 · 发布于 2024-04-24 11:03:23

你的蜘蛛逻辑很混乱:

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

为了说明当前爬虫程序什么都不做的原因,请参见此图: enter image description here

相关问题 更多 >