Python爬虫之UserAgent的使用实例
作者:LittleQueue 时间:2022-03-10 15:41:39
问题: 在Python爬虫的过程中经常要模拟UserAgent, 因此自动生成UserAgent十分有用, 最近看到一个Python库(fake-useragent),可以随机生成各种UserAgent, 在这里记录一下, 留给自己爬虫使用。
安装 pip install fake-useragent
使用案例
基本使用
from fake_useragent import UserAgent
ua = UserAgent()
ua.ie
# Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US);
ua.msie
# Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)'
ua['Internet Explorer']
# Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)
ua.opera
# Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11
ua.chrome
# Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'
ua.google
# Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13
ua['google chrome']
# Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
ua.firefox
# Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1
ua.ff
# Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1
ua.safari
# Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25
# and the best one, random via real world browser usage statistic
ua.random
注意:
fake-useragent 将收集到的数据缓存到temp文件夹, 例如 /tmp, 更新数据:
from fake_useragent import UserAgent
ua = UserAgent()
ua.update()
有时候会因为网络或者其他问题,出现异常(fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached
), 可以禁用服务器缓存(从这里踩了一个坑, 没仔细看文档的锅):
from fake_useragent import UserAgent
ua = UserAgent(use_cache_server=False)
可以自己添加本地数据文件(v0.1.4+)
import fake_useragent
# I am STRONGLY!!! recommend to use version suffix
location = '/home/user/fake_useragent%s.json' % fake_useragent.VERSION
ua = fake_useragent.UserAgent(path=location)
ua.random
其他功能用到的也不是很多,详细见文档吧。
来源:https://blog.csdn.net/anonymous_qsh/article/details/79349068
标签:python,爬虫,useragent
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python钉钉机器人运维脚本监控实例
2022-08-23 22:19:48
![](https://img.aspxhome.com/file/2023/5/81855_0s.jpg)
sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)
2011-09-30 11:49:22
Python+OpenCV实现在图像上绘制矩形
2023-07-27 15:29:58
![](https://img.aspxhome.com/file/2023/4/66894_0s.jpg)
js高亮关键词系列方法
2008-01-22 11:11:00
python3 批量获取对应端口服务的实例
2021-07-14 11:52:05
用 iframe 解决下拉框与层之冲突
2008-04-28 12:24:00
![](https://img.aspxhome.com/file/UploadPic/20071/2007131102430882s.gif)
Python内置函数—vars的具体使用方法
2021-05-07 04:09:57
Python中的Classes和Metaclasses详解
2022-07-08 09:28:47
楼层数横排比竖排好
2008-04-26 07:28:00
![](https://img.aspxhome.com/file/UploadPic/20084/26/200842673425574s.jpg)
利用django+wechat-python-sdk 创建微信服务器接入的方法
2023-06-04 21:28:55
![](https://img.aspxhome.com/file/2023/3/81853_0s.jpg)
python Elasticsearch索引建立和数据的上传详解
2021-03-25 23:26:35
![](https://img.aspxhome.com/file/2023/3/76913_0s.png)
三分钟掌握PHP操作数据库
2023-06-01 01:15:43
![](https://img.aspxhome.com/file/2023/9/55279_0s.png)
PHP函数原理理解详谈
2023-06-04 01:56:59
![](https://img.aspxhome.com/file/2023/0/55360_0s.png)
SQL查询效率-100w数据查询只要1秒
2008-08-20 18:25:00
深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复
2023-11-06 19:25:27
ORACLE常用数值函数、转换函数、字符串函数
2023-07-21 02:03:40
python防止栈溢出的实例讲解
2022-07-31 17:17:15
灵活调用xsl来解析xml文档(js异步)
2008-09-05 17:12:00
python assert断言的实例用法
2023-06-24 22:15:42
python自动分箱,计算woe,iv的实例代码
2022-10-13 22:51:50
![](https://img.aspxhome.com/file/2023/3/88013_0s.jpg)