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

scrapy基础知识之随机切换fake-useragent 库的使用:

时间:2017-06-10 18:21:42      阅读:2098      评论:0      收藏:0      [点我收藏+]

标签:ice   log   github   rmi   close   one   scrapy   logs   init   

pip install fake-useragent
from fake_useragent import UserAgent
ua = UserAgent()

middlewares.py
技术分享
 1 from fake_useragent import UserAgent
 2 
 3 class RandomUserAgent(object):
 4     # def __init__(self,crawl):
 5     #     super(RandomUserAgent,self).__init__()
 6     #     self.ua=UserAgent()
 7     def process_request(self, request, spider):
 8         #useragent = random.choice(USER_AGENTS)
 9         ua=UserAgent()
10         request.headers.setdefault("User-Agent",ua.random)
View Code

settints.py

技术分享
1 DOWNLOADER_MIDDLEWARES = {
2       LaGou.middlewares.RandomUserAgent: 1,
3 #    ‘LaGou.middlewares.MyCustomDownloaderMiddleware‘: 543,
4 }
View Code

 更多访问地址:https://github.com/hellysmile/fake-useragent

scrapy基础知识之随机切换fake-useragent 库的使用:

标签:ice   log   github   rmi   close   one   scrapy   logs   init   

原文地址:http://www.cnblogs.com/huwei934/p/6979286.html

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