【python 爬虫】伪造UA字符串
来源:互联网 发布:消息队列java 编辑:程序博客网 时间:2024/06/12 00:36
写好爬虫的原则只有一条:
就是让你的抓取行为和用户访问网站的真实行为尽量一致。
1、伪造UA字符串,每次请求都使用随机生成的UA。
为了减少复杂度,随机生成UA的功能通过第三方库fake-useragent实现
pip install fake-useragent
2、生成一个UA字符串只需要如下代码:
核心代码:
from fake_useragent import UserAgentua=UserAgent()print(ua.random)
ipython 环境下:
In [1]: from fake_useragent import UserAgentIn [2]: ua=UserAgent()No handlers could be found for logger "root"In [3]: ua.randomOut[3]: u'Mozilla/5.0 (Windows; U; Windows NT 6.0; tr-TR) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5'In [4]: ua.randomOut[4]: u'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36'In [5]: ua.randomOut[5]: u'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36'In [6]: ua.randomOut[6]: u'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36'In [7]: ua.randomOut[7]: u'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36'In [8]: ua.randomOut[8]: u'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36'In [9]:
阅读全文
1 0
- 【python 爬虫】伪造UA字符串
- [备忘]Python网络爬虫伪造报头
- Python 爬虫一些常用的UA(user-agent)
- python爬虫之伪造报头来采集网页信息
- 修改User-Agent伪造爬虫
- python伪造udp数据包
- 爬虫 禁止访问时伪造字符头
- php采集模拟点击伪造IP,伪造浏览器useragent伪造来源防反爬虫例子--
- UA
- Python爬虫处理xml和html的字符串
- python多线程爬虫学习--去除字符串中间空格
- python爬虫-->爬虫基础
- [爬虫] Python爬虫技巧
- Pyspider中给爬虫伪造随机请求头
- 使用python怎样检测伪造视频?
- 如何用 Python 检测伪造的视频
- Python爬虫
- python 爬虫
- Deep Deterministic Policy Gradient(DDPG)
- List of results of algorithms on some well know datasets
- Spring入门篇二(2)
- scanf() 的返回值
- 素数筛选 + 下标当值
- 【python 爬虫】伪造UA字符串
- 5000 的阶乘
- [OpenGL] 利用Shader实现复杂地形的渲染
- javaEE之JSP
- 深度学习常用优化方法
- Codeforces 101206 H & HDU 6006 Engineer Assignment
- LeetCode#513 Find Bottom Left Tree Value题解(C++版)
- Java 构造方法的修饰符
- eclipse与虚拟内存