当我们大量使用scrapy 请求一个网站的时候我们知道scrapy默认的请求头是:

可以看到默认的请求头就是scrapy,这样就很容易导致浏览器封掉的可能,这个时候我们就需要给请求加一个请求头来模拟浏览器,一般的可以这样做:

可以直接在spider文件中添加custom_settings 这个设置,代码如下:
custom_settings = { 'LOG_LEVEL': 'DEBUG', 'LOG_FILE': '5688_log_%s.txt' % time.
time(), // 配置的日志 "DEFAULT_REQUEST_HEADERS": { 'User-Agent': 'Mozilla/5.0
(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/67.0.3396.99 Safari/537.36', } } // 添加的请求头
当然也可以随机的添加请求头,步骤如下:
第一步, 在settings文件中添加一些UserAgent,在这里笔者是查找别人的:
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/4.0 (compatible; MSIE 7.0; Windows
NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727;
SE 2.X MetaSr 1.0)", "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/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)", "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" ]
同时在settings文件中设置 “DOWNLOADER_MIDDLEWARES”
DOWNLOADER_MIDDLEWARES = { # 'lagou.middlewares.LagouDownloaderMiddleware': 543
,'lagou(项目的名称).middlewares.RandomUserAgentMiddleware': 400, }
第二步骤, 在 middlewares.py 文件中导入 settings模块中的 USER_AGENT_LIST 方法
from lagou.settings import USER_AGENT_LIST
同时在这个文件中添加以下的操作:
class RandomUserAgentMiddleware(object): def process_request(self, request,
spider): rand_use = random.choice(USER_AGENT_LIST) if rand_use:
request.headers.setdefault('User-Agent', rand_use)
图片显示:


这样就可以了。

原创,转载需标明作者和来源。违者必究。