scrapy_ip_agent
#File name is rotate_useragent
# -*- coding: UTF-8 -*-
import random
import urllib2
import redis
from scrapy.contrib.downloadermiddleware.useragent import UserAgentMiddleware
from CrawlerTools.ScrapyFileSystem.config import *
class RotateUserAgentMiddleware(UserAgentMiddleware):
def __inti__(self,user_agent=""):
self.user_agent=user_agent
def process_request(self,request,spider):
user_agent_list=["Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 "
"(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 "
"(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 "
"(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 "
"(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 "
"(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 "
"(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 "
"(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 "
"(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 "
"(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24" ]
ua=random.choice(user_agent_list)
if ua:
request.headers.setdefault("User-Agent",ua)
#Get ip
cfg=config("Redis")
redisId=redis.Redis(cfg["host"],cfg["port"],1,cfg["pwd"])
res=redisId.srandmember("ipPool",1)[0].strip()
urls=res.split(":")
request.meta['proxy'] ="http://"+str(urls[0])+":"+str(urls[1])
# Use the following lines if your proxy requires authentication
#Configuration profile
DOWNLOADER_MIDDLEWARES = {
'scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware' : None,
'CrawlerTools.rotate_useragent.RotateUserAgentMiddleware' :400
}
scrapy_ip_agent的更多相关文章
随机推荐
- J2EE到底是什么
目前所有的B/S系统应用可以分为:有状态(statefull)和无状态(stateless)两大类别. 有状态是指在整个系统的处理过程中要保留记住一些信息,而无状态则相反,每次request都是独立的 ...
- oracle 字符集转换:AL32UTF8->ZHS16GBK
select userenv('language') from dual; --修改Oracle数据库字符集为ZHS16GBK : SQL>conn / as sysdba; SQL>sh ...
- Web开发人员需知的Web缓存知识
最近的译文距今已有4年之久,原文有一定的更新.今天踩着前辈们的肩膀,再次把这篇文章翻译整理下.一来让自己对web缓存的理解更深刻些,二来让大家注意力稍稍转移下,不要整天HTML5, 面试题啊叨啊叨的~ ...
- quartz 时间配置规则
quartz 时间配置规则 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 0-59 , - * / ...
- UVALive 7281 Saint John Festival (凸包+O(logn)判断点在凸多边形内)
Saint John Festival 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/J Description Porto's ...
- C#经典面试题 C# 中 Struct 与 Class 的区别,以及两者的适用场合
在一家公司面试时,第一个问题就是问到这个 转载 文章 http://www.cnblogs.com/waitrabbit/archive/2008/05/18/1202064.html 来解释此问题 ...
- Edit Box多行显示时如何使滚动条始终在下方
两种方法: ① CEdit *pEdit = ((CEdit*)GetDlgItem(IDC_EDIT_RXDATA)); pEdit->LineScroll(pEdit->GetLin ...
- 使用apt-get autoremove造成的系统无法开机
由于误操作(apt-get autoremove xxx)删除了一些lib文件貌似,之后,系统直接重启,然后就无法进入系统,后使用引导盘对系统进行修复,思路如下: 1.挂载已经有的分区,挂载为可读可写 ...
- PicklingError: Can't pickle <type 'generator'>: it's not found as __builtin_
多进程传递 参数时,需要是python系统已知的,不然不知道怎么序列化
- rxjava各种使用场景
1. 数据的三级缓存 final Observable memory = Observable.create(new Observable.OnSubscribe() { @Override publ ...