python爬虫之Scrapy 使用代理配置——乾颐堂
在爬取网站内容的时候,最常遇到的问题是:网站对IP有限制,会有防抓取功能,最好的办法就是IP轮换抓取(加代理)
下面来说一下Scrapy如何配置代理,进行抓取
1.在Scrapy工程下新建“middlewares.py”
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# Importing base64 library because we'll need it ONLY in case if the proxy we are going to use requires authentication 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) request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass |
2.在项目配置文件里(./pythontab/settings.py)添加
|
1
2
3
4
|
DOWNLOADER_MIDDLEWARES = { 'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 110, 'pythontab.middlewares.ProxyMiddleware': 100, } |
http://www.qytang.com/cn/list/28/611.htm
http://www.qytang.com/cn/list/28/610.htm
http://www.qytang.com/cn/list/28/595.htm
http://www.qytang.com/cn/list/28/583.htm
http://www.qytang.com/cn/list/28/582.htm
http://www.qytang.com/cn/list/28/576.htm
http://www.qytang.com/cn/list/28/523.htm
http://www.qytang.com/cn/list/28/499.htm
http://www.qytang.com/cn/list/28/488.htm
http://www.qytang.com/cn/list/28/466.htm
python爬虫之Scrapy 使用代理配置——乾颐堂的更多相关文章
- python爬虫之Scrapy 使用代理配置
转载自:http://www.python_tab.com/html/2014/pythonweb_0326/724.html 在爬取网站内容的时候,最常遇到的问题是:网站对IP有限制,会有防抓取功能 ...
- python生成验证码,文字转换为图片-乾颐堂
在58或者赶集等一些网站上经常看到手机号是图片格式,或者一些网站的验证码.这些都是动态生成的,今天我们来看一下如何用python把文字生成图片.其实今天主要借助pygame的图像渲染模块,这样比较简单 ...
- python多线程编程5: 条件变量同步-乾颐堂
互斥锁是最简单的线程同步机制,Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还 ...
- python 探测网站目录的GUI程序-乾颐堂
1.pyqt4写的界面 find_ui.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...
- python移除系统多余大文件-乾颐堂
文件多了乱放, 突然有一天发现硬盘空间不够了, 于是写了个python脚本搜索所有大于10MB的文件,看看这些大文件有没有重复的副本,如果有,全部列出,以便手工删除 使用方式 加一个指定目录的参数 比 ...
- python实现高效率的排列组合算法-乾颐堂
组合算法 本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 代表的数被选中,为0则没选中. 首先初始化,将数组前n个元素置1,表示第一个组合为前n个数. 然后从左到右扫描数组 ...
- Python将阿拉伯数字转化为中文大写-乾颐堂
利用Python将阿拉伯数字转化为中文大写,其实最麻烦的地方就是中间空多个0的问题,这种情况下,采用拆分法则,将一个大数字,先拆分成整数部分和小数部分,再对整数部分按照仟.万.亿.兆分位拆分为四个字符 ...
- Python 执行js的2种解决方案-乾颐堂
第1种方案 SpiderMonkey是Mozilla项目的一部分,是一个用C语言实现的JavaScript脚本引擎, 该引擎分析.编译和执行脚本,根据JS数据类型和对象的需要进行内存分配及释放操作:利 ...
- Python守护进程(多线程开发)-乾颐堂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
随机推荐
- 将xml转为array
/** * 将xml转为array * @param string $xml * @throws Exception */ public function FromXml($xml) { if (!$ ...
- elasticSearch Java Spring Data Api
1. BoolQueryBuilder qb=QueryBuilders. boolQuery(); qb.should(QueryBuilders.matchQuery("keyWord& ...
- 线程组ThreadGroup
ThreadGroup线程组表示一个线程的集合.此外,线程组也可以包含其他线程组. 线程组构成一棵树,在树中,除了初始线程组外,每个线程组都有一个父线程组. 允许线程访问有关自己的线程组的信息,但 ...
- kubernetes 学习 常用命令
1 kubectl get nodes #查看nodes节点情况 2 kubectl describe node node_name_XXXX # 查看nodes详 ...
- node的express中间件之bodyParser
bodyParser用于解析客户端请求的body中的内容,内部使用JSON编码处理,url编码处理以及对于文件的上传处理. 下面是一个文件上传的例子. 建立一个1.html页面 <!DOCTYP ...
- netcore中使用log4net日志
第一.控制台程序中使用log4net static void Main(string[] args) { ILoggerRepository repository = LoggerManager.C ...
- django Chinese
http://usyiyi.cn/translate/django_182/contents.html
- Web(Easy UI)
HTML菜鸟教程 http://www.runoob.com/html/html-tables.html EasyUI combobox下拉多选框的实现 https://www.cnblogs.com ...
- RPC框架的服务注册和发现
https://www.cnblogs.com/valor-xh/p/6281502.html https://blog.csdn.net/listslim1/article/details/5157 ...
- nginx实现多个域名共享80端口
server { listen 80; server_name server8085.duchong.cn; location / { proxy_pass http://127.0.0.1:8085 ...