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 ...
随机推荐
- VS编译错误:fatal error C1859:unexpected precompiled header error, simply rerunning the compiler might fix this problem
fatal error C1859:unexpected precompiled header error, simply rerunning the compiler might fix this ...
- 转载 -- Cocoapod方式引入百川SDK -报错[!] Unable to find a specification for `xxx`
[cocopad集成百川sdk官网] iOS需要填写BundleID .BundleID要是当前应用的BundleID.勾选淘宝登录基础包下载SDK. 注意事项:将下载的SDK中的身份图片yw_122 ...
- JQuery------实现鼠标点击和滑动不同效果
如图: 代码: html <ul class="price-brand-right"> @foreach (Brand item in ViewBag.Brand) { ...
- shell脚本学习总结01--文件描述符和重定向
文件描述符是与文件输入和输出的相关联的整数,它们用来追踪已打开的文件,文件描述符0,1,2是系统预留的. 0 --> stdin (标准输入) 1 --> stdout (标准输出) 2 ...
- springboot整合mybatis之用外置服务器启动项目(二)
在上一篇中我们是用的springboot自带的tomcat服务器,接下来想试一下 将springboot当做一个web项目 放在eclipse中用tomcat来启动. 首先在pom.xml中加上,移除 ...
- 81、去除标题栏 Activity 和 AppCompatActivity
[Activity ] requestWindowFeature(Window.FEATURE_NO_TITLE); [AppCompatActivity] getSupportActionBar() ...
- 如何用sql语句复制一张表
如何用sql语句复制一张表 1.复制表结构及数据到新表 CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete ...
- __construct __destory __call __get __set
1,__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用. 我们知道 php5对象模型 < ,所以__construct()作为类的默认的构造函数 而不会调用同类名函 ...
- 用httpclient做压力测试时Too many open files的解决办法
在工作过程中,用httpclient去压测一个web api,发现压一小段时间就出现了Too many open files.实际上,HttpClient建立Socket时 ,post.release ...
- vue兄弟组件传值
vue中除了父子组件传值,父传子用props,子传父用$emit,有时候兄弟组件之间也需要传值 1. 先定义一个中间件,src下面新建self.js import Vue from 'vue'; le ...