• fake_useragent


    fake_useragent

    fake_useragent第三方库,来实现随机请求头的设置

    安装

    pip3 install fake-useragent
    

    用法

    from fake_useragent import UserAgent
    ua = UserAgent()
    print(ua.ie)
    print(ua.opera)
    print(ua.chrome)
    print(ua.firefox)
    print(ua.safari)
    print(ua.random)    获取任意请求头
    

    报错:

    fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached
    

    解决

    • 禁用服务器缓存
    ua = UserAgent(use_cache_server=False)
    
    • 忽略ssl验证
    ua = UserAgent(verify_ssl=False)
    
    • 使用json文件
    1、wget https://fake-useragent.herokuapp.com/browsers/0.1.11
    2、mv 0.1.11 fake_useragent_0.1.11.json
    
    ua = UserAgent(path="./fake_useragent_0.1.11.json")  #path后面跟下载的json文件路径
    

    #! /usr/bin/python3
    # -*- congfig:utf-8 -*-
    
    from fake_useragent import UserAgent
    
    def test():
        ua = UserAgent()
        ua = UserAgent(path="./fake_useragent_0.1.11.json")
        return ua.random
    if __name__ == "__main__":
        for i in range(5):
            print(test())
    
    Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36
    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17
    Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36
    Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0
    Mozilla/5.0 (Windows; U; Windows NT 6.0; ja-JP) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
    
  • 相关阅读:
    Java8学习笔记(五)--Stream API详解[转]
    Java8学习笔记(四)--接口增强
    Java8学习笔记(三)--方法引入
    JAVA8学习笔记(二)----三个预定义接口
    JAVA8学习笔记(一)----Lambda表达式
    Java基础加强总结(三)——代理(Proxy)
    Java基础加强总结(二)——泛型
    mysql统计表的大小
    jquery异步上传图片
    瀑布流
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/13443755.html
Copyright © 2020-2023  润新知