python scrapy parse()函数,返回值返回到哪里?

2024-09-21 11:35:59 发布

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

我是新来的,如果这个问题无关紧要,我很抱歉。我已经从官方网页上看到了这个文件。当我浏览文档时,我遇到了这个例子:

import scrapy
from myproject.items import MyItem

class MySpider(scrapy.Spider):
  name = ’example.com’
  allowed_domains = [’example.com’]
  start_urls = [
  ’http://www.example.com/1.html’,
  ’http://www.example.com/2.html’,
  ’http://www.example.com/3.html’,
  ]

  def parse(self, response):
    for h3 in response.xpath(’//h3’).extract():
      yield MyItem(title=h3)
    for url in response.xpath(’//a/@href’).extract():
      yield scrapy.Request(url, callback=self.parse) 

我知道,parse方法必须返回一个项或/和请求,但是这些返回值返回到哪里?

一个是一个项,另一个是请求,我认为这两种类型的处理方式不同,在CrawlSpider的情况下,它有回调规则。这个回调的返回值呢?去哪里?与parse()相同?

我很困惑的程序,即使我读了文件。。。。


Tags: 文件inimportselfcomhttpforparse
1条回答
网友
1楼 · 发布于 2024-09-21 11:35:59

根据documentation

The parse() method is in charge of processing the response and returning scraped data (as Item objects) and more URLs to follow (as Request objects).

换言之,返回/产生的项和请求的处理方式不同,项被传递给项管道和项导出器,但请求被放入Scheduler中,该Downloader通过管道将请求传递给Downloader以发出请求并返回响应。然后,引擎接收响应并将其交给spider处理(给callback方法)。

整个数据流过程以非常详细的方式在Architecture Overview页中描述。

希望能有所帮助。

相关问题 更多 >

    热门问题