在scrapy下载中间键middlewares.py中,写下如下代码,然后在设置文件settings.py设置
DOWNLOADER_MIDDLEWARES = {
# 'testip.middlewares.TestipDownloaderMiddleware': 543,
'testip.middlewares.RandomUserAgent': 10,
'testip.middlewares.RandomProxy': 100
}
如何我们还就只需要在setting中添加代理ip和User-Agent
具体格式如下:
PROXIES = [
{'ip_port': '121.13.29.28:8888','user_passwd': '1'},
{'ip_port': '166.111.80.162:3128','user_passwd': '1'},
{'ip_port': '119.27.177.169:80','user_passwd': '1'},
{'ip_port': '121.58.17.52:80','user_passwd': '1'},
{'ip_port': '121.58.17.52:80','user_passwd': '1'},
{'ip_port': '140.205.222.3:80','user_passwd': '1'},
{'ip_port': '112.17.65.133:8060','user_passwd': '1'},
{'ip_port': '43.247.68.211:3128','user_passwd': '1'},
{'ip_port': '61.4.184.180:3128','user_passwd': '1'},
{'ip_port': '120.25.61.10:8080','user_passwd': '1'},
{'ip_port': '119.27.177.169:80','user_passwd': '1'},
{'ip_port': '61.135.217.7:80','user_passwd': '1'},
{'ip_port': '61.135.217.7:80','user_passwd': '1'},
{'ip_port': '124.193.37.5:8888','user_passwd': '1'},
{'ip_port': '146.185.147.157:8080','user_passwd': '1'},
{'ip_port': '143.137.146.130:8080','user_passwd': '1'},
{'ip_port': '114.215.103.121:8081','user_passwd': '1'},
{'ip_port': '101.53.101.172:9999','user_passwd': '1'},
{'ip_port': '106.75.23.58:8888','user_passwd': '1'},
{'ip_port': '114.112.104.223:80','user_passwd': '1'},
{'ip_port': '116.11.254.37:80','user_passwd': '1'},
{'ip_port': '119.28.112.130:3128','user_passwd': '1'},
{'ip_port': '119.28.26.57:3128','user_passwd': '1'},
]
USER_AGENTS = [
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
"Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5"
]
这些都是免费ip,可以在网上找到,推荐使用爬虫来维护一个代理池,明天写一个利用Flask和redis数据库
来维护的代理池,如何就可以直接使用中间件访问数据库来拿取ip
对于USER_AGENTS可以直接拿去使用
import random
import base64
from scrapy import signals
from testip.settings import USER_AGENTS
from testip.settings import PROXIES
# 随机的User-Agent
class RandomUserAgent(object):
def process_request(self, request, spider):
useragent = random.choice(USER_AGENTS)
request.headers.setdefault("User-Agent", useragent)
class RandomProxy(object):
def process_request(self, request, spider):
proxy = random.choice(PROXIES)
if proxy['user_passwd'] == '1':
# 没有代理账户验证的代理使用方式
request.meta['proxy'] = "http://" + proxy['ip_port']
else:
# 对账户密码进行base64编码转换
base64_userpasswd = base64.b64encode(proxy['user_passwd'])
# 对应到代理服务器的信令格式里
request.headers['Proxy-Authorization'] = 'Basic ' + base64_userpasswd
request.meta['proxy'] = "http://" + proxy['ip_port']