python - 為什么用requests庫能爬取而用scrapy卻不能?
問題描述
# -*- coding: utf-8 -*-import requestsdef xici_request(): url = ’http://www.xicidaili.com’ headers = {’Accept’: ’text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8’,’Accept-Encoding’: ’gzip, deflate, sdch’,’Accept-Language’: ’zh-CN,zh;q=0.8’,’Cache-Control’: ’max-age=0’,’Connection’: ’keep-alive’,’Host’: ’www.xicidaili.com’,’Referer’: ’https://www.google.com/’,’User-Agent’: ’Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36’ res = requests.get(url, headers=headers) print(res.text)if __name__ == ’__main__’: xici_request()
# -*- coding: utf-8 -*-import scrapyfrom collectips.items import CollectipsItemclass XiciSpider(scrapy.Spider): name = 'xici' allowed_domains = ['http://www.xicidaili.com'] headers = {’Accept’: ’text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8’, ’Accept-Encoding’: ’gzip, deflate, sdch’, ’Accept-Language’: ’zh-CN,zh;q=0.8’, ’Cache-Control’: ’max-age=0’, ’Connection’: ’keep-alive’, ’Host’: ’www.xicidaili.com’, ’Referer’: ’https://www.google.com/’, ’User-Agent’: ’Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36’} def start_requests(self):reqs = []for i in range(1, 21): req = scrapy.Request(’http://www.xicidaili.com/nn/{}’.format(i), headers=self.headers) reqs.append(req)return reqs def parse(self, response):item = CollectipsItem()sel = response.selectorfor i in range(2, 102): item[’IP’] = sel.xpath(’//*[@id='ip_list']/tbody/tr[{}]/td[2]/text()’.format(i)).extract() item[’PORT’] = sel.xpath(’//*[@id='ip_list']/tbody/tr[{}]/td[3]/text()’.format(i)).extract() item[’DNS_POSITION’] = sel.xpath(’//*[@id='ip_list']/tbody/tr[{}]/td[4]/a/text()’.format(i)).extract() item[’TYPE’] = sel.xpath(’//*[@id='ip_list']/tbody/tr[{}]/td[6]/text()’.format(i)).extract() item[’SPEED’] = sel.xpath(’//*[@id='ip_list']/tbody/tr[{}]/td[7]/p[@title]’.format(i)).extract() item[’LAST_CHECK_TIME’] = sel.xpath(’//*[@id='ip_list']/tbody/tr[{}]/td[10]/text()’.format(i)).extract() yield item
代碼如上,為什么requests能返回網(wǎng)頁內(nèi)容,而scrapy卻是報錯內(nèi)部服務(wù)器錯誤500? 請大神解救??
問題解答
回答1:并發(fā)你沒考慮進(jìn)去吧,當(dāng)同一時間發(fā)起過多的請求會直接封你IP
相關(guān)文章:
1. JavaScript將.apply()與'new'運(yùn)算符配合使用這可能嗎?2. angular.js - webpack build后的angularjs路由跳轉(zhuǎn)問題3. java - web項目中,用戶登陸信息存儲在session中好 還是cookie中好,取決于什么?4. java - Activity中的成員變量被賦值之后,Activity被回收的時候內(nèi)存才會被釋放嗎5. 請求一個數(shù)據(jù)返回內(nèi)容為空或者錯誤如何再次請求幾次6. 數(shù)組按鍵值封裝!7. 老師,flex-shrink: 1; 按視頻操作,不會自動縮放8. 為什么bindClass訪問不了的?9. Discuz! Q 有人用過嗎?10. php由5.3升級到5.6后,登錄網(wǎng)站,返回的是php代碼,不是登錄界面,各位大神有知道的嗎?
