fake-useragent,python爬虫伪装请求头
在编写爬虫进行网页数据的时候,大多数情况下,需要在请求是增加请求头,下面介绍一个python下非常好用的伪装请求头的库:fake-useragent,具体使用说明如下:
1.在scrapy中的使用
第一步
pip install fake-useragent
第二步:在middlewares中配置下载中间件。
class RandomUserAgentMiddleware(object):
#随机更换user_agent
def __init__(self, crawler):
super(RandomUserAgentMiddleware,self).__init__()
self.ua = UserAgent()
self.ua_type = crawler.settings.get("RANDOM_UA_TYPE","random")#为了随机获得浏览器类型 @classmethod
def from_crawler(cls, crawler):
return cls(crawler)#导入crawler,从而获取其他的配置文件里的数据 def process_request(self,request,spider):
def get_ua():
return getattr(self.ua,self.ua_type)#获取ua的ua_type属性,也就是获得random request.headers.setdefault('User-Agent',get_ua())
第三步:在settings中配置
RANDOM_UA_TYPE = "random"
USER_AGENT ='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
DEFAULT_REQUEST_HEADERS = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
'User-Agent':USER_AGENT,
}
2不是在scrapy中的使用
pip install fake-useragent
获取各浏览器的fake-useragent
from fake_useragent import UserAgent
ua = UserAgent()
#ie浏览器的user agent
print(ua.ie) #opera浏览器
print(ua.opera) #chrome浏览器
print(ua.chrome) #firefox浏览器
print(ua.firefox) #safri浏览器
print(ua.safari) #最常用的方式
#写爬虫最实用的是可以随意变换headers,一定要有随机性。支持随机生成请求头
print(ua.random)
print(ua.random)
print(ua.random)
示例代码
from fake_useragent import UserAgent
import requests
ua=UserAgent()
#请求的网址
url="https://www.baidu.com"
#请求头
headers={"User-Agent":ua.random}
#请求网址
response=requests.get(url=url,headers=headers)
#响应体内容
print(response.text)
#响应状态信息
print(response.status_code)
#响应头信息
print(response.headers)
fake-useragent,python爬虫伪装请求头的更多相关文章
- python爬虫添加请求头和请求主体
添加头部信息有两种方法 1.通过添加urllib.request.Request中的headers参数 #先把要用到的信息放到一个字典中 headers = {} headers['User-Agen ...
- python爬虫添加请求头
request import requests headers = { # 'Accept': 'application/json, text/javascript, */*; q=0.01', # ...
- Python:fake-useragent 伪装请求头
写爬虫的时候,在进行 request 请求的时候,多数情况下需要添加请求头,否则就不能正常请求. 添加请求头最常用的做法是修改 User-Agent 来伪装浏览器. 以前在写请求头的时候,都是通过 c ...
- python爬虫#网络请求requests库
中文文档 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html requests库 虽然Python的标准库中 urlli ...
- python 爬虫001-http请求过程
HTTP 请求流程 一次完整的HTTP请求过程从TCP三次握手建立连接成功后开始,客户端按照指定的格式开始向服务端发送HTTP请求,服务端接收请求后,解析HTTP请求,处理完业务逻辑,最后返回一个HT ...
- python爬虫伪装技术应用
版权声明:本文为博主原创文章,转载 请注明出处: https://blog.csdn.net/sc2079/article/details/82423865 -写在前面 本篇博客主要是爬虫伪装技术的应 ...
- Python爬虫requests请求库
requests:pip install request 安装 实例: import requestsurl = 'http://www.baidu.com'response = requests. ...
- python 爬虫 伪装
#coding=utf-8 import requests def requests_view(response): import webbrowser requests_url = response ...
- python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性)
python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性) 目录 随机User-Agent 获取代理ip 检测代理ip可用性 随机User-Agent fake_usera ...
随机推荐
- 《linux系统及其编程》实验课记录(五)
实验 5:权限的设置和更改 实验环境: 安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统.有另外一个无特权用户 student,密码 student 的 ...
- keyword static
1. 不能通过类名来调用类的非静态成员函数 2. 类的对象可以使用静态成员函数和非静态成员函数 3. 静态成员函数中不能引用非静态成员 因为静态成员函数属于整个类, 在类的实例化对象之前就已经分配了空 ...
- iOS 开发之 -- UDID和UUID的详解
老实说,搞了几年的ios开发了,对基础的概念,还是不牢固,整天都是为了赶进度而码代码,这里记录一下这两者的区别: UDID的全名为 Unique Device Identifier :设备唯一标识符. ...
- IOS 开发之--获取真机的deviceToeken
获取真机的devicetoken的方法: #pragma mark 注册APNs成功并上报DeviceToken - (void)application:(UIApplication *)applic ...
- Cocos Studio编辑器运行日志路径
有时候打开Cocos Studio直接崩溃,要想查找原因就要查log了,网上找了好久才找到日志路径,在你工作空间下面的CocosStudio2\logs. 工作空间就是安装时候设置的那个路径.
- ios 开发证书制作
1.首先打开Mac中的keychain 选择钥匙串访问-证书助手-创建证书-导出CertificateSigningRequest.CertSigningRequest.CertSigningRequ ...
- Android 全局异常处理(三)
用过安卓手机的用户以及安卓开发者们会时长碰到程序异常退出的情况,普通用户遇到这种情况,肯定非常恼火,甚至会骂一生垃圾软件,然后卸载掉.那么开发者们在开发过程中遇到这种情况给怎么办呢,当然,你不可能世界 ...
- 01.Elasticsearch安装
1.下载运行Elasticsearch 1.下载解压elasticsearch Elasticsearch官网地址:https://www.elastic.co/ Elasticsearch最新版下载 ...
- Sass (Syntactically Awesome StyleSheets)
官网:https://www.sass.hk/docs/ Sass 是一款强化 CSS 的辅助工具,它在 CSS 语法的基础上增加了变量 (variables).嵌套 (nested rules).混 ...
- HDU_5534_Partial Tree
Partial Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...