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卻是報(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. 在mac下出現(xiàn)了兩個(gè)docker環(huán)境2. 如何解決docker宿主機(jī)無法訪問容器中的服務(wù)?3. HTML5不支持frameset一般怎么解決?4. javascript - Vue v-for判斷是否為第4列,然后加個(gè)橫線或者第4行才顯示這一個(gè)<li>5. javascript - react native在run-android時(shí)出現(xiàn)這個(gè)錯(cuò)誤該怎么解決?大神賜教6. javascript - 如何獲取點(diǎn)擊事件點(diǎn)擊后前一個(gè)后一個(gè)的值。7. css3像卷軸一樣展開8. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????9. css - 為何box的顏色沒有變?10. css - C#與java開發(fā)Windows程序哪個(gè)好?
