User Agent
中文名爲用戶代理,是Http協議中的一部分,屬於頭域的組成部分,User Agent也簡稱UA
。它是一個特殊字符串頭,是一種向訪問網站提供你所使用的瀏覽器類型及版本、操做系統及版本、瀏覽器內核、等信息的標識;css
User-Agent
是headers
中的一個屬性,表示當前訪問服務器的身份信息,若是同一個身份過於頻繁的訪問服務器會被識別爲機器身份,遭到反爬的打擊,因此須要頻繁的更改User-Agent信息;html
User-Agent字段解析:瀏覽器標識 (操做系統標識; 加密等級標識; 瀏覽器語言) 渲染引擎標識 版本信息;node
想要隨機更改User-Agent,首先咱們能夠在蜘蛛文件的Spider類中添加一個header請求頭,不少網站只須要userAgent信息就能夠經過,可是有的網站還須要驗證一些其餘的信息,因此咱們能夠在請求頭中添加一些須要用到的字段,好比:web
Accept
:客戶端支持的數據類型,用逗號隔開,是有順序的,分號前面是主類型,分號後是子類型;瀏覽器
Accept-Encoding
:指定瀏覽器能夠支持的web服務器返回內容壓縮編碼類型;服務器
Accept-Language
:瀏覽器可接受的天然語言的類型;app
Connection
:設置HTTP鏈接的持久化,一般都是Keep-Alive;dom
host
:服務器的域名或IP地址,若是不是通用端口,還包含該端口號;ide
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字段;