亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于爬蟲中scrapy.Request的更多參數(shù)用法

 更新時間:2022年07月15日 09:42:04   作者:黑馬藍汐  
這篇文章主要介紹了關(guān)于爬蟲中scrapy.Request的更多參數(shù)用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

爬蟲中scrapy.Request的更多參數(shù)

scrapy.Request的參數(shù)

scrapy.Request(url[,callback,method="GET",headers,body,cookies,meta,dont_filter=Fallse])

參數(shù)解釋:

中括號中的參數(shù)為可選參數(shù),可寫可不寫

  • callback:表示當前的url響應交給哪個函數(shù)去處理(默認為parse函數(shù))
  • meta:實現(xiàn)數(shù)據(jù)在不同解析函數(shù)中傳遞,meta默認帶有部分數(shù)據(jù),比如下載延遲、請求深度等(用于解析方法之間的數(shù)據(jù)傳遞,常用在一條數(shù)據(jù)分散在多個不同結(jié)構(gòu)的頁面中的情況)
  • dont_filter:默認為False,會過濾請求的url地址,即請求過的url地址不會繼續(xù)被請求,對需要重復請求的url地址可以把它設置為True,start_urls中的地址會被反復請求,否則程序不會啟動
  • headers:接收一個字典,其中不包括cookies
  • cookies:接收一個字典,專門放置cookies
  • method:指定POST或GET請求
  • body:接收json字符串,為post的數(shù)據(jù)發(fā)送payload_post請求

meta參數(shù)

meta的作用:meta可以實現(xiàn)數(shù)據(jù)在不同的解析函數(shù)中的傳遞

在爬蟲文件的parse方法中,增加一個函數(shù)parse_detail函數(shù)(用來解析另一個頁面):

def parse(self,response):
? ? ...
? ? yield scrapy.Request(detail_url, callback=self.parse_detail,meta={"item":item})
...

def parse_detail(self,response):
? ? #獲取之前傳入的item
? ? item = resposne.meta["item"]

就相當于,把parse中解析的數(shù)據(jù)存到了meta字典中,對應的key為item;而在另一個函數(shù)(parse_detail)中,通過meta字典中的key:item來提取parse中的數(shù)據(jù),從而實現(xiàn)不同頁面數(shù)據(jù)的拼接

注意:

  • meta參數(shù)是一個字典
  • meta字典中有一個固定的鍵proxy,表示代理ip

scrapy中Request中常用參數(shù)

  • url: 就是需要請求,并進行下一步處理的url
  • callback: 指定該請求返回的Response,由那個函數(shù)來處理。
  • method: 一般不需要指定,使用默認GET方法請求即可
  • headers: 請求時,包含的頭文件。一般不需要。內(nèi)容一般如下:使用 urllib2 自己寫過爬蟲的肯定知道
Host: media.readthedocs.org
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: text/css,*/*;q=0.1
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://scrapy-chs.readthedocs.org/zh_CN/0.24/
Cookie: _ga=GA1.2.1612165614.1415584110;
Connection: keep-alive
If-Modified-Since: Mon, 25 Aug 2014 21:59:35 GMT
Cache-Control: max-age=0
  • meta: 比較常用,在不同的請求之間傳遞數(shù)據(jù)使用的。字典dict型
request_with_cookies = Request(url="http://www.example.com",
? ? ? ?cookies={'currency': 'USD', 'country': 'UY'},
? ? ? ?meta={'dont_merge_cookies': True})
  • encoding: 使用默認的 'utf-8' 就行。

dont_filter: indicates that this request should not be filtered by the scheduler. 
     This is used when you want to perform an identical request multiple times, 
     to ignore the duplicates filter. Use it with care, or you will get into crawling loops. 
     Default to False.

  • errback: 指定錯誤處理函數(shù)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論