码迷,mamicode.com
首页 > 其他好文 > 详细

scrapy随机更改User-Agent方法

时间:2017-12-03 20:56:43      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:user-agent更改   scrapy   python   

1       Scrapy设置User-Agent

1.1   settings.py文件中设置USER_AGENT参数即可实现

1.1.1  使用固定User-Agent方法

settings.py文件中加入如下代码即可(USER_AGENT是固定写法),具体使用什么agent可以自己更改下面蓝色字体内容。

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文件中通过创建一个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)实现设置agent方法

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

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

1.2   通过写middleware方法实现

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库设置User-Agent方法

安装fake-useragent(useragent的库)

pip install fake-useragent

 

middlewares.py文件中导入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,
}


scrapy随机更改User-Agent方法

标签:user-agent更改   scrapy   python   

原文地址:http://blog.51cto.com/laoyinga/2046970

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!