pip install fake-useragent
from fake_useragent import UserAgent
ua = UserAgent()
middlewares.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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)
settints.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 DOWNLOADER_MIDDLEWARES = { 2 'LaGou.middlewares.RandomUserAgent': 1, 3 # 'LaGou.middlewares.MyCustomDownloaderMiddleware': 543, 4 }
更多访问地址:https://github.com/hellysmile/fake-useragent