掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
介紹

成都創(chuàng)新互聯(lián)公司成立10多年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁設(shè)計(jì)、國際域名空間、網(wǎng)絡(luò)營銷、VI設(shè)計(jì)、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。網(wǎng)站是否美觀、功能強(qiáng)大、用戶體驗(yàn)好、性價(jià)比高、打開快等等,這些對(duì)于網(wǎng)站建設(shè)都非常重要,成都創(chuàng)新互聯(lián)公司通過對(duì)建站技術(shù)性的掌握、對(duì)創(chuàng)意設(shè)計(jì)的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。
Request類是一個(gè)http請(qǐng)求的類,對(duì)于爬蟲而言是一個(gè)很重要的類。通常在Spider中創(chuàng)建這樣的一個(gè)請(qǐng)求,在Downloader中執(zhí)行這樣的一個(gè)請(qǐng)求。同時(shí)也有一個(gè)子類FormRequest繼承于它,用于post請(qǐng)求。
在Spider中通常用法:
- yield scrapy.Request(url = 'zarten.com')
類屬性和方法有:
Request
- class scrapy.http.Request(url[, callback, method='GET', headers, body, cookies, meta, encoding='utf-8', priority=0, dont_filter=False, errback, flags])
參數(shù)說明:
- cookies = {'name1' : 'value1' , 'name2' : 'value2'}
list方式:
- cookies = [
- {'name': 'Zarten', 'value': 'my name is Zarten', 'domain': 'example.com', 'path': '/currency'}
- ]
- from scrapy.spidermiddlewares.httperror import HttpError
- from twisted.internet.error import DNSLookupError
- from twisted.internet.error import TimeoutError, TCPTimedOutError
- class ToScrapeCSSSpider(scrapy.Spider):
- name = "toscrape-css"
- # start_urls = [
- # 'http://quotes.toscrape.com/',
- # ]
- start_urls = [
- "http://www.httpbin.org/", # HTTP 200 expected
- "http://www.httpbin.org/status/404", # Not found error
- "http://www.httpbin.org/status/500", # server issue
- "http://www.httpbin.org:12345/", # non-responding host, timeout expected
- "http://www.httphttpbinbin.org/", # DNS error expected
- ]
- def start_requests(self):
- for u in self.start_urls:
- yield scrapy.Request(u, callback=self.parse_httpbin,
- errback=self.errback_httpbin,
- dont_filter=True)
- def parse_httpbin(self, response):
- self.logger.info('Got successful response from {}'.format(response.url))
- # do something useful here...
- def errback_httpbin(self, failure):
- # log all failures
- self.logger.info(repr(failure))
- # in case you want to do something special for some errors,
- # you may need the failure's type:
- if failure.check(HttpError):
- # these exceptions come from HttpError spider middleware
- # you can get the non-200 response
- response = failure.value.response
- self.logger.info('HttpError錯(cuò)誤 on %s', response.url)
- elif failure.check(DNSLookupError):
- # this is the original request
- request = failure.request
- self.logger.info('DNSLookupError錯(cuò)誤 on %s', request.url)
- elif failure.check(TimeoutError, TCPTimedOutError):
- request = failure.request
- self.logger.info('TimeoutError錯(cuò)誤 on %s', request.url)
- yield scrapy.Request(url = 'zarten.com', meta = {'name' : 'Zarten'})
在Response中:
- my_name = response.meta['name']
不過也有scrapy內(nèi)置的特殊key,也非常有用,它們?nèi)缦拢?/p>
可以設(shè)置http或https代理
- request.meta['proxy'] = 'https://' + 'ip:port'
- yield scrapy.Request(url= 'https://httpbin.org/get/zarten', meta= {'handle_httpstatus_list' : [404]})
在parse函數(shù)中可以看到處理404錯(cuò)誤:
- def parse(self, response):
- print('返回信息為:',response.text)
- def start_requests(self):
- urls = ['http://quotes.toscrape.com/page/1',
- 'http://quotes.toscrape.com/page/3',
- 'http://quotes.toscrape.com/page/5',
- ]
- for i ,url in enumerate(urls):
- yield scrapy.Request(urlurl= url, meta= {'cookiejar' : i})
- def parse(self, response):
- next_page_url = response.css("li.next > a::attr(href)").extract_first()
- if next_page_url is not None:
- yield scrapy.Request(response.urljoin(next_page_url), meta= {'cookiejar' : response.meta['cookiejar']}, callback= self.parse_next)
- def parse_next(self, response):
- print('cookiejar:', response.meta['cookiejar'])
- def start_requests(self):
- headers = {
- 'user-agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
- }
- yield scrapy.Request(url= 'https://www.amazon.com', headersheaders= headers)
- def parse(self, response):
- print('響應(yīng)時(shí)間為:', response.meta['download_latency'])
FormRequest
FormRequest 類為Request的子類,用于POST請(qǐng)求
這個(gè)類新增了一個(gè)參數(shù) formdata,其他參數(shù)與Request一樣,詳細(xì)可參考上面的講述
一般用法為:
- yield scrapy.FormRequest(url="http://www.example.com/post/action",
- formdata={'name': 'Zarten', 'age': '27'},
- callback=self.after_post)

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流