python scrapy和requests区别_python,_为什么用requests库能爬取而用scrapy却不能?,python - phpStudy...

为什么用requests库能爬取而用scrapy却不能?

# -*- coding: utf-8 -*-

import requests

def 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 scrapy

from collectips.items import CollectipsItem

class 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.selector

for 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]/div[@title]'.format(i)).extract()

item['LAST_CHECK_TIME'] = sel.xpath(

'//*[@id="ip_list"]/tbody/tr[{}]/td[10]/text()'.format(i)).extract()

yield item

代码如上,为什么requests能返回网页内容,而scrapy却是报错内部服务器错误500? 请大神解救??

相关阅读:

nodejs怎么遍历req.session.user

JS检测设备方向疑问

用connect-multer上传文件,form中的req.body.photo.name不能访问

mysql存储过程

http的client无法为https设置header。

为什么我安装imagick之后,php -m可以看到安装成功,phpinfo看不到呢?确定用的是同一个版本的php

vue v-for ul 组件的数组数据如何动态赋值

vsCode 如何支持es6/es7

angularjs 点击空白处关闭

js这个哪里写的不对呢?

tp框架问题

新手问下怎么触发这段代码。。

express,router.route的req.session与app.use下面的req.session不一样

我的代码提交就是出错,不知道哪里错了?

VideoCapture读取视频中的一帧怎么保存为图片(imwrite使用出错)?

为什么我用命令行连接mysql时可以连上,但是用程序连接就连不上了呢?

python模拟登陆微博Element is not currently interactable

vuex 是怎么把 store 放到全局的。

我怎么觉得flex中justify-content和align-items两个属性反了呢

有没有免费的央视及各地卫视节目时间表接口?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值