溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

scrapy隨機更改User-Agent方法

發(fā)布時間:2020-05-27 19:08:06 來源:網(wǎng)絡(luò) 閱讀:11215 作者:老鷹a 欄目:安全技術(shù)

1       Scrapy設(shè)置User-Agent

1.1   settings.py文件中設(shè)置USER_AGENT參數(shù)即可實現(xiàn)

1.1.1  使用固定User-Agent方法

settings.py文件中加入如下代碼即可(USER_AGENT是固定寫法),具體使用什么agent可以自己更改下面藍色字體內(nèi)容。

USER_AGENT = '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)

1.1.2  使用隨機User-Agent方法

settings.py文件中通過創(chuàng)建一個agent列表,然后通過從中隨機取出1個賦值給USER_AGENT變量即可

代碼如下:

import random
UserAgentList = [
   
"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)",
   
]

USER_AGENT = random.choice(UserAgentList)

1.1.3  使用第三方user-agent庫(fake-useragent)實現(xiàn)設(shè)置agent方法

settings.py文件中加入如下代碼:

from fake_useragent import UserAgent
ua = UserAgent()
USER_AGENT = ua.random

1.2   通過寫middleware方法實現(xiàn)

1.2.1  通過在setting文件中自己編寫user-agent方法

setting.py文件中寫一個UserAgentList列表

UserAgentList = [
   
'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1500.55 Safari/537.36',
   
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17'
]

 

setting.py文件中啟用DOWNLOADER_MIDDLEWARES,并且把默認的注釋掉

DOWNLOADER_MIDDLEWARES = {
   
#'amazon.middlewares.MyCustomDownloaderMiddleware': 543,
   
'amazon.middlewares.MyUserAgentMiddleware': 543,
}

 

middlewares.py文件中增加以下代碼:

rom scrapy.downloadermiddlewares.useragent import UserAgentMiddleware
from amazon.settings import UserAgentList
import random
class MyUserAgentMiddleware(UserAgentMiddleware):
   
def __init__(self, user_agent=''):
       
self.user_agent = user_agent

   
def process_request(self, request, spider):
        agent = random.choice(
list(UserAgentList))
        request.headers[
'User-Agent'] = agent

 

1.2.2  利用fake-useragent庫設(shè)置User-Agent方法

安裝fake-useragent(useragent的庫)

pip install fake-useragent

 

middlewares.py文件中導(dǎo)入fake-useragent

from fake_useragent import UserAgent

 

middlewares.py文件中編寫一個middleware中間件,

 

from fake_useragent import UserAgent
from scrapy.downloadermiddlewares.useragent import UserAgentMiddleware
class MyUserAgentMiddleware(UserAgentMiddleware):
   
def __init__(self, user_agent=''):
       
self.user_agent = user_agent

   
def process_request(self, request, spider):
       
ua = UserAgent()
        request.headers[
'User-Agent'] = ua.random

setting.py文件中啟用DOWNLOADER_MIDDLEWARES,并且把默認的注釋掉

DOWNLOADER_MIDDLEWARES = {
   
#'amazon.middlewares.MyCustomDownloaderMiddleware': 543,
   
'amazon.middlewares.MyUserAgentMiddleware': 543,
}


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI