Python3中生成随机UserAgent -- 通过fake_useragent

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

GitHub ---> https://github.com/hellysmile/fake-useragent

安装 ---> pip3 install fake-useragent

查看useragent ---> http://fake-useragent.herokuapp.com/browsers/0.1.5

关键是后面的版本号,如果更新后使用原版本号就查看不到useragent;

如何操作最新版本号?通过pip3 list 查看安装的版本号;

使用代码

from fake_useragent import UserAgent

ua = UserAgent()
print(ua.ie)   #随机打印ie浏览器任意版本
print(ua.firefox) #随机打印firefox浏览器任意版本
print(ua.chrome)  #随机打印chrome浏览器任意版本
print(ua.random)  #随机打印任意厂家的浏览器

下面致谢的博文中,还总结了fake-useragent在scrapy中的使用。
致谢
Python3中通过fake_useragent生成随机UserAgent

posted @ 2020-03-07 22:40  songbiao  阅读(21)  评论(0编辑  收藏  举报