1. 创建middlewares.py
文件。
Scrapy代理IP、Uesr-Agent的切换都是通过DOWNLOADER_MIDDLEWARES
进行控制,我们在settings.py
同级目录下创建middlewares.py
文件,包装所有请求。
import random
import base64
from settings import USER_AGENTS
from 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‘] is None:
# 没有代理账户验证的代理使用方式
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‘]
2. 修改settings.py配置USER_AGENTS和PROXIES
添加USER_AGENTS:
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设置PROXIES:
免费代理IP可以网上搜索,或者付费购买一批可用的私密代理IP:
PROXIES = [
{‘ip_port‘: ‘111.8.60.9:8123‘, ‘user_passwd‘: ‘user1:pass1‘},
{‘ip_port‘: ‘101.71.27.120:80‘, ‘user_passwd‘: ‘user2:pass2‘},
{‘ip_port‘: ‘122.96.59.104:80‘, ‘user_passwd‘: ‘user3:pass3‘},
{‘ip_port‘: ‘122.224.249.122:8088‘, ‘user_passwd‘: ‘user4:pass4‘},
]
除非特殊需要,禁用cookies,防止某些网站根据Cookie来封锁爬虫。
COOKIES_ENABLED = False
设置下载延迟
DOWNLOAD_DELAY = 3
最后设置setting.py里的DOWNLOADER_MIDDLEWARES,添加自己编写的下载中间件类。
DOWNLOADER_MIDDLEWARES = {
#‘mySpider.middlewares.MyCustomDownloaderMiddleware‘: 543,
‘mySpider.middlewares.RandomUserAgent‘: 1,
‘mySpider.middlewares.ProxyMiddleware‘: 100
}
为什么HTTP代理要使用base64编码:
HTTP代理的原理很简单,就是通过HTTP协议与代理服务器建立连接,协议信令中包含要连接到的远程主机的IP和端口号,如果有需要身份验证的话还需要加上授权信息,服务器收到信令后首先进行身份验证,通过后便与远程主机建立连接,连接成功之后会返回给客户端200,表示验证通过,就这么简单,下面是具体的格式
CONNECT 59.64.128.198:21 HTTP/1.1 Host: 59.64.128.198:21 Proxy-Authorization: Basic bGV2I1TU5OTIz User-Agent: OpenFetion
其中Proxy-Authorization
是身份验证信息,Basic后面的字符串是用户名和密码组合后进行base64编码的结果,也就是对username:password进行base64编码。
HTTP/1.0 200 Connection established
OK,客户端收到收面的信令后表示成功建立连接,接下来要发送给远程主机的数据就可以发送给代理服务器了,代理服务器建立连接后会在根据IP地址和端口号对应的连接放入缓存,收到信令后再根据IP地址和端口号从缓存中找到对应的连接,将数据通过该连接转发出去。
时间: 2024-11-05 17:25:42