标签:读取 baidu erb request 缓存 form AC scrapy one
伪装浏览器代理 在爬取网页是有些服务器对请求过滤的不是很高可以不用ip来伪装请求直接将自己的浏览器信息给伪装也是可以的。
1.在setting.py文件中加入以下内容,这是一些浏览器的头信息
USER_AGENT_LIST = [‘zspider/0.9-dev http://feedback.redkolibri.com/‘,
‘Xaldon_WebSpider/2.0.b1‘,
‘Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) Speedy Spider (http://www.entireweb.com/about/search_tech/speedy_spider/)‘,
‘Mozilla/5.0 (compatible; Speedy Spider; http://www.entireweb.com/about/search_tech/speedy_spider/)‘,
‘Speedy Spider (Entireweb; Beta/1.3; http://www.entireweb.com/about/search_tech/speedyspider/)‘,
‘Speedy Spider (Entireweb; Beta/1.2; http://www.entireweb.com/about/search_tech/speedyspider/)‘,
‘Speedy Spider (Entireweb; Beta/1.1; http://www.entireweb.com/about/search_tech/speedyspider/)‘,
‘Speedy Spider (Entireweb; Beta/1.0; http://www.entireweb.com/about/search_tech/speedyspider/)‘,
‘Speedy Spider (Beta/1.0; www.entireweb.com)‘,
‘Speedy Spider (http://www.entireweb.com/about/search_tech/speedy_spider/)‘,
‘Speedy Spider (http://www.entireweb.com/about/search_tech/speedyspider/)‘,
‘Speedy Spider (http://www.entireweb.com)‘,
‘Sosospider+(+http://help.soso.com/webspider.htm)‘,
‘sogou spider‘,
‘Nusearch Spider (www.nusearch.com)‘,
‘nuSearch Spider (compatible; MSIE 4.01; Windows NT)‘,
‘lmspider (lmspider@scansoft.com)‘,
‘lmspider lmspider@scansoft.com‘,
‘ldspider (http://code.google.com/p/ldspider/wiki/Robots)‘,
‘iaskspider/2.0(+http://iask.com/help/help_index.html)‘,
‘iaskspider‘,
‘hl_ftien_spider_v1.1‘,
‘hl_ftien_spider‘,
‘FyberSpider (+http://www.fybersearch.com/fyberspider.php)‘,
‘FyberSpider‘,
‘everyfeed-spider/2.0 (http://www.everyfeed.com)‘,
‘envolk[ITS]spider/1.6 (+http://www.envolk.com/envolkspider.html)‘,
‘envolk[ITS]spider/1.6 ( http://www.envolk.com/envolkspider.html)‘,
‘Baiduspider+(+http://www.baidu.com/search/spider_jp.html)‘,
‘Baiduspider+(+http://www.baidu.com/search/spider.htm)‘,
‘BaiDuSpider‘,
‘Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0) AddSugarSpiderBot www.idealobserver.com‘,
]
2.在spider同级目录下建立一个MidWare文件价里面写一个HeaderMidWare.py文件 内容为
# encoding: utf-8
from scrapy.utils.project import get_project_settings
import random
settings = get_project_settings()
class ProcessHeaderMidware():
"""process request add request info"""
def process_request(self, request, spider):
"""
随机从列表中获得header, 并传给user_agent进行使用
"""
ua = random.choice(settings.get(‘USER_AGENT_LIST‘))
spider.logger.info(msg=‘now entring download midware‘)
if ua:
request.headers[‘User-Agent‘] = ua
# Add desired logging message here.
spider.logger.info(u‘User-Agent is : {} {}‘.format(request.headers.get(‘User-Agent‘), request))
pass
3.在setting.py文件中添加
DOWNLOADER_MIDDLEWARES = {
‘projectName.MidWare.HeaderMidWare.ProcessHeaderMidware‘: 543,
}
fake_userAgent是github上的开源项目
1.安装fake_userAgent
pip install fake-useragent
2.在spider同级目录下建立一个MidWare文件价里面写一个user_agent_middlewares.py文件内容为
# -*- coding: utf-8 -*-
from fake_useragent import UserAgent
class RandomUserAgentMiddlware(object):
#随机跟换user-agent
def __init__(self,crawler):
super(RandomUserAgentMiddlware,self).__init__()
self.ua = UserAgent()
self.ua_type = crawler.settings.get(‘RANDOM_UA_TYPE‘,‘random‘)#从setting文件中读取RANDOM_UA_TYPE值
@classmethod
def from_crawler(cls,crawler):
return cls(crawler)
def process_request(self,request,spider): ###系统电泳函数
def get_ua():
return getattr(self.ua,self.ua_type)
# user_agent_random=get_ua()
request.headers.setdefault(‘User_Agent‘,get_ua())
pass
3.在setting.py中添加
RANDOM_UA_TYPE = ‘random‘##random chrome
DOWNLOADER_MIDDLEWARES = {
‘projectName.MidWare.user_agent_middlewares.RandomUserAgentMiddlware‘: 543,
‘scrapy.downloadermiddlewares.useragent.UserAgentMiddleware‘:None,
}
fake_userAgent伪装代理就配置好了,与第一种方法相比不用写一大串的浏览器头,那些浏览器头会在https://fake-useragent.herokuapp.com/browsers/0.1.7 中得到。
在第一次启用fake_userAgent的时候会有一些错,我认为是项目请求网络时需要缓存一些内容而导致的。
github地址:https://github.com/sea1234/fake-useragent
Python scrapy 伪装代理和fake_userAgent的使用
标签:读取 baidu erb request 缓存 form AC scrapy one
原文地址:https://www.cnblogs.com/hellangels333/p/8918938.html