自己写爬虫时买的代理ip有密码,在网上查了都是下面这种:

1、在Scrapy工程下新建"middlewares.py":

import base64

# Start your middleware class

class ProxyMiddleware(object):

# overwrite process request

def process_request(self, request, spider):

# Set the location of the proxy

request.meta['proxy'] = "http://YOUR_PROXY_IP:PORT"

# Use the following lines if your proxy requires authentication

proxy_user_pass = "USERNAME:PASSWORD"

# setup basic authentication for the proxy

encoded_user_pass = base64.encodestring(proxy_user_pass)  #我用的python3.5,这个地方报错 TypeError: expected bytes-like object, not str

request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass

2、在项目配置文件里setting.py添加:

DOWNLOADER_MIDDLEWARES = {

...   #此处省略其他中间件配置

'ScrapyRedisTest.middlewares.ProxyMiddleware': 100,

}

================================解决方法如下:(只改动标红的两行,其他的都不变)================================================

import base64

# Start your middleware class

class ProxyMiddleware(object):

# overwrite process request

def process_request(self, request, spider):

# Set the location of the proxy

request.meta['proxy'] = "http://YOUR_PROXY_IP:PORT"

# Use the following lines if your proxy requires authentication

proxy_user_pass = "USERNAME:PASSWORD"

# setup basic authentication for the proxy

encoded_user_pass = base64.b64encode(proxy_user_pass.encode('utf-8'))

request.headers['Proxy-Authorization'] = 'Basic ' + str(encoded_user_pass, encoding="utf-8")

2、在项目配置文件里setting.py添加:

DOWNLOADER_MIDDLEWARES = {

...   #此处省略其他中间件配置

'ScrapyRedisTest.middlewares.ProxyMiddleware': 100,

}

scrapy怎么设置带有密码的代理ip base64.encodestring不能用 python3.5,base64库里面的encodestring()被换成了什么?的更多相关文章

  1. Python 爬虫的代理 IP 设置方法汇总

    本文转载自:Python 爬虫的代理 IP 设置方法汇总 https://www.makcyun.top/web_scraping_withpython15.html 需要学习的地方:如何在爬虫中使用 ...

  2. 突破IP限制动态替换代理ip。

    须要导入的两个jar包 实现的javabean <span style="font-size:18px;">package com.jx.po; public clas ...

  3. scrapy框架设置代理ip,headers头和cookies

    [设置代理ip] 根据最新的scrapy官方文档,scrapy爬虫框架的代理配置有以下两种方法: 一.使用中间件DownloaderMiddleware进行配置使用Scrapy默认方法scrapy s ...

  4. scrapy的User-Agent中间件、代理IP中间件、cookies设置、多个爬虫自定义settings设置

    在scrapy的反爬中,常用的几个配置,简单总结了下: User-Agent中间件: from fake_useragent import UserAgent class RandomUserAgen ...

  5. python scrapy 爬取西刺代理ip(一基础篇)(ubuntu环境下) -赖大大

    第一步:环境搭建 1.python2 或 python3 2.用pip安装下载scrapy框架 具体就自行百度了,主要内容不是在这. 第二步:创建scrapy(简单介绍) 1.Creating a p ...

  6. python+scrapy 爬取西刺代理ip(一)

    转自:https://www.cnblogs.com/lyc642983907/p/10739577.html 第一步:环境搭建 1.python2 或 python3 2.用pip安装下载scrap ...

  7. Scrapy爬取美女图片第三集 代理ip(下)

    这是我的公众号获取原创保护的首篇文章,原创的肯定将支持我继续前行.现在写这篇文章的时间是晚上11:30,写完就回寝室休息了,希望更多的朋友与我一起同行(当然需要一个善良的妹子的救济).(我的新书< ...

  8. Scrapy 框架 中间件 代理IP 提高效率

    中间件 拦截请求跟响应 进行ua(User-Agent ) 伪装 代理 IP 中间件位置: 引擎 和下载器 中间 的中间件 ( 下载中间件) 引擎 跟 spider 中间 的中间件 ( 爬虫中间件)( ...

  9. 把你的Centos设置成代理ip服务器

    前言:最近在公司做爬虫相关的工作,做过数据抓取的都知道,写程序抓取数据的过程并不像平常我们用浏览器打开网页那么简单!大多数的网站为了自己站点的性能和数据安全都设置了各种反爬策略.最常见的就是添加验证码 ...

随机推荐

  1. 爬虫3_python2

    # coding=utf-8 import urllib params=urllib.urlencode({'t':1,'eggs':2,'bacon':0})#现在大多数网站都是动态网页,需要你动态 ...

  2. Python -- 函数之推导式

    5.12 推导式 l = [] for i in range(1,11): l.append(i) print(l) # 用列表推导式 (一行搞定) l = [i for i in range(1,1 ...

  3. js函数式编程(二)-柯里化

    这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...

  4. 安装mysqlclient失败

    环境:python3.6 sudo apt-get install python3.6-dev sudo apt-get install default-libmysqlclient-dev 参考:h ...

  5. 安装ElasticSearch 6.1.1 head插件

    https://blog.csdn.net/zoubf/article/details/79007908 主要参考了这个blog 才完成所有的配置,很好的参考资料

  6. ccf 201803-4 棋局评估(Python实现)

    一.原题 问题描述 试题编号: 201803-4 试题名称: 棋局评估 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 Alice和Bob正在玩井字棋游戏. 井字棋游戏的规则很 ...

  7. 【mysql】mysql存储过程实例

    ```mysql DELIMITER $$   DROP PROCEDURE IF EXISTS `system_number_update` $$   CREATE DEFINER=`root`@` ...

  8. 中移物联网onenet入门学习笔记1:资料获取

    onenet学习资料.视频.例程汇总:https://open.iot.10086.cn/bbs/thread-977-1-1.html onenet开发文档:https://open.iot.100 ...

  9. octave-basic

    在coursera上斯坦福的machine learning,lecturer极力推荐开源的编程环境Octave入手,所以我也下载了来试一试吧== 参考链接:http://www.linuxdiyf. ...

  10. 用\r做出进度条

    在做ftp作业的时候,需要做一个上传和下载的进度条,做的时候发现用\r很容易就能做出来 def show_progress(self, has, total): rate = float(has) / ...