python - 為什么用requests庫(kù)能爬取而用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)頁(yè)內(nèi)容,而scrapy卻是報(bào)錯(cuò)內(nèi)部服務(wù)器錯(cuò)誤500? 請(qǐng)大神解救??
問題解答
回答1:并發(fā)你沒考慮進(jìn)去吧,當(dāng)同一時(shí)間發(fā)起過多的請(qǐng)求會(huì)直接封你IP
相關(guān)文章:
1. mysql - 一個(gè)表和多個(gè)表是多對(duì)多的關(guān)系,該怎么設(shè)計(jì)2. 一個(gè)mysql聯(lián)表查詢的問題3. html5 - iOS的webview加載出來的H5網(wǎng)頁(yè),怎么修改html標(biāo)簽select的樣式字體?4. python - django 里自定義的 login 方法,如何使用 login_required()5. java - 我現(xiàn)在一個(gè)servlet中有調(diào)用socket訪問,作為socket的客戶端,6. mysql主從 - 請(qǐng)教下mysql 主動(dòng)-被動(dòng)模式的雙主配置 和 主從配置在應(yīng)用上有什么區(qū)別?7. 主從備份 - 跪求mysql 高可用主從方案8. python如何不改動(dòng)文件的情況下修改文件的 修改日期9. javascript - git clone 下來的項(xiàng)目 想在本地運(yùn)行 npm run install 報(bào)錯(cuò)10. python 如何實(shí)現(xiàn)PHP替換圖片 鏈接
