W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
請(qǐng)求的回調(diào)是一個(gè)函數(shù),在下載請(qǐng)求的響應(yīng)時(shí)將調(diào)用該函數(shù)。將使用下載的 ?Response
? 對(duì)象作為其第一個(gè)參數(shù)。
例子::
def parse_page1(self, response):
return scrapy.Request("http://www.example.com/some_page.html",
callback=self.parse_page2)
def parse_page2(self, response):
# this would log http://www.example.com/some_page.html
self.logger.info("Visited %s", response.url)
在某些情況下,您可能對(duì)向這些回調(diào)函數(shù)傳遞參數(shù)感興趣,以便稍后在第二個(gè)回調(diào)中接收這些參數(shù)。下面的示例演示如何通過(guò)使用 ?Request.cb_kwargs
? 屬性:
def parse(self, response):
request = scrapy.Request('http://www.example.com/index.html',
callback=self.parse_page2,
cb_kwargs=dict(main_url=response.url))
request.cb_kwargs['foo'] = 'bar' # add more arguments for the callback
yield request
def parse_page2(self, response, main_url, foo):
yield dict(
main_url=main_url,
other_url=response.url,
foo=foo,
)
警告
?Request.cb_kwargs
? 在版本中引入 ?1.7
? . 在此之前,使用 ?Request.meta
? 建議在回調(diào)時(shí)傳遞信息。后 ?1.7
? , ?Request.cb_kwargs
? 成為處理用戶信息的首選方式,離開(kāi) ?Request.meta
? 用于與中間件和擴(kuò)展等組件通信。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: