Scrapy 随机UA模板

from fake_useragent import UserAgent   ##随机UA头
class RandomUserAgentMiddleware(object):
    def process_request(self, request, spider):
        ua = UserAgent()
        request.headers['User-Agent'] = ua.random
        #输出随机UA
        # print(ua.random) -->添加自定义的UA,给request的headers赋值即可