User Agent
中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称
UA
。它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及版本、操作系统及版本、浏览器内核、等信息的标识;
User-Agent
是
headers
中的一个属性,表示当前访问服务器的身份信息,如果同一个身份过于频繁的访问服务器会被识别为机器身份,遭到反爬的打击,所以需要频繁的更改User-Agent信息;
User-Agent字段解析:浏览器标识 (操作系统标识; 加密等级标识; 浏览器语言) 渲染引擎标识 版本信息;
想要随机更改User-Agent,首先我们可以在蜘蛛文件的Spider类中添加一个header请求头,很多网站只需要userAgent信息就可以通过,但是有的网站还需要验证一些其他的信息,所以我们可以在请求头中添加一些需要用到的字段,比如:
Accept
:客户端支持的数据类型,用逗号隔开,是有顺序的,分号前面是主类型,分号后是子类型;
Accept-Encoding
:指定浏览器可以支持的web服务器返回内容压缩编码类型;
Accept-Language
:浏览器可接受的自然语言的类型;
Connection
:设置HTTP连接的持久化,通常都是Keep-Alive;
host
:服务器的域名或IP地址,如果不是通用端口,还包含该端口号;
Referer
:指当前请求的URL是在什么地址引用的;
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'host': 'dribbble.com/stories',
'Referer': 'https://dribbble.com/',
}
user_agent_list = [
"Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16",
"Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14",
"Mozilla/5.0 (Windows NT 6.0; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 12.14",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0) Opera 12.14",
"Opera/12.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.02",
"Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.00",
"Opera/9.80 (Windows NT 5.1; U; zh-sg) Presto/2.9.181 Version/12.00",
"Opera/12.0(Windows NT 5.2;U;en)Presto/22.9.168 Version/12.00",
"Opera/12.0(Windows NT 5.1;U;en)Presto/22.9.168 Version/12.00",
"Mozilla/5.0 (Windows NT 5.1) Gecko/20100101 Firefox/14.0 Opera/12.0",
"Opera/9.80 (Windows NT 6.1; WOW64; U; pt) Presto/2.10.229 Version/11.62",
"Opera/9.80 (Windows NT 6.0; U; pl) Presto/2.10.229 Version/11.62",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; de) Presto/2.9.168 Version/11.52",
"Opera/9.80 (Windows NT 5.1; U; en) Presto/2.9.168 Version/11.51",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; de) Opera 11.51",
"Opera/9.80 (X11; Linux x86_64; U; fr) Presto/2.9.168 Version/11.50",
"Opera/9.80 (X11; Linux i686; U; hu) Presto/2.9.168 Version/11.50",
"Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11",
"Opera/9.80 (X11; Linux i686; U; es-ES) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/5.0 Opera 11.11",
"Opera/9.80 (X11; Linux x86_64; U; bg) Presto/2.8.131 Version/11.10",
"Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10",
"Opera/9.80 (Windows NT 5.1; U; zh-tw) Presto/2.8.131 Version/11.10",
"Opera/9.80 (Windows NT 6.1; Opera Tablet/15165; U; en) Presto/2.8.149 Version/11.1",
"Opera/9.80 (X11; Linux x86_64; U; Ubuntu/10.10 (maverick); pl) Presto/2.7.62 Version/11.01",
]
import random
def get_randam_int(lst):
return random.randint(0, len(lst)-1)
def parse(self, response):
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'host': 'dribbble.com',
'Referer': 'https://dribbble.com/',
}
random_index = get_randam_int(user_agent_list)
random_agent = user_agent_list[random_index]
headers['User-Agent'] = random_agent
a_nodes = response.css('header div.teaser a')
for a_node in a_nodes:
# print(a_node)
a_url = a_node.css('::attr(href)').extract()[0]
a_image_url = a_node.css('img::attr(src)').extract()[0]
yield Request(headers=headers,url=parse.urljoin(response.url, a_url), callback=self.parse_analyse, meta={'a_image_url': a_image_url})
打开你需要爬虫的网页;
按键盘的F12或手动去浏览器右上角的“更多工具”选项选择开发者工具;
按键盘的F5刷新网页;
选择Network中的Doc;
点击Headers,就可以在最末尾查看Request Headers的User-Agent字段,也可以复制使用User-Agent字段;
参考: https://www.9xkd.com/user/plan-view.html?id=1782598054
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。