目标:掌握怎么用 Python 库 fake_useragent(非标准库) 生成随机的用户代理


    一:怎么用 Python 库 fake_useragent(非标准库)生成随机的用户代理

    安装:pip3 install fake_useragent
    功能:随机生成用户代理(User-Agent)

    语法:

    导入模块:from fake_useragent import UserAgent
    生成随机代理:UserAgent().random

    爬虫用法:

    导入模块:from fake_useragent import UserAgent
    生成随机代理:headers = {“User-Agent”:UserAgent().random}

    注意事项:生成随机用户代理时,利用的是UserAgent方法,不是random方法,random只是属性!!!

    截图演示:

    image.png