在编写爬虫进行网页数据的时候,大多数情况下,需要在请求是增加请求头,下面介绍一个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爬虫伪装请求头的更多相关文章

  1. python爬虫添加请求头和请求主体

    添加头部信息有两种方法 1.通过添加urllib.request.Request中的headers参数 #先把要用到的信息放到一个字典中 headers = {} headers['User-Agen ...

  2. python爬虫添加请求头

    request import requests headers = { # 'Accept': 'application/json, text/javascript, */*; q=0.01', # ...

  3. Python:fake-useragent 伪装请求头

    写爬虫的时候,在进行 request 请求的时候,多数情况下需要添加请求头,否则就不能正常请求. 添加请求头最常用的做法是修改 User-Agent 来伪装浏览器. 以前在写请求头的时候,都是通过 c ...

  4. python爬虫#网络请求requests库

    中文文档 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html requests库 虽然Python的标准库中 urlli ...

  5. python 爬虫001-http请求过程

    HTTP 请求流程 一次完整的HTTP请求过程从TCP三次握手建立连接成功后开始,客户端按照指定的格式开始向服务端发送HTTP请求,服务端接收请求后,解析HTTP请求,处理完业务逻辑,最后返回一个HT ...

  6. python爬虫伪装技术应用

    版权声明:本文为博主原创文章,转载 请注明出处: https://blog.csdn.net/sc2079/article/details/82423865 -写在前面 本篇博客主要是爬虫伪装技术的应 ...

  7. Python爬虫requests请求库

    requests:pip install  request 安装 实例: import requestsurl = 'http://www.baidu.com'response = requests. ...

  8. python 爬虫 伪装

    #coding=utf-8 import requests def requests_view(response): import webbrowser requests_url = response ...

  9. python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性)

    python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性) 目录 随机User-Agent 获取代理ip 检测代理ip可用性 随机User-Agent fake_usera ...

随机推荐

  1. ThinkPHP种where的使用(_logic and _complex)的使用实例

    1.对于thinkphp中的 and ,or 等复合型的查询,我要正确的使用相关的方法. a.实例 b.实例

  2. 浅谈AutoResetEvent的用法

    using System;using System.Threading; namespace AutoResetEvent_Examples{    class MyMainClass    {    ...

  3. hdu 1560(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 思路:关键是启发式函数h()的构造,我们可以这样想:每次给主串增加一个字符和字符串的最后一位比较 ...

  4. IOS 开发之 -- 获取本机通讯录里面所有的联系人,并传到后台

    项目中遇到一个需求,就是需要在入口的时候,获取通讯录的权限,并把所有的联系人,以接口参数的形式传到后台,通过网上查资料,历时3个小时,终于完成, 话不多,直接上代码: 1,导入系统库 #import ...

  5. IOS 开发之--获取真机的deviceToeken

    获取真机的devicetoken的方法: #pragma mark 注册APNs成功并上报DeviceToken - (void)application:(UIApplication *)applic ...

  6. Python Scrapy 自动爬虫注意细节(3)

    一.对指定页面爬取 yield Request(url, meta={'cookiejar': response.meta['cookiejar']}, callback=self.parse_url ...

  7. 将java项目发布到本地的linux虚拟机上

    1.首先安装虚拟机,这里就不介绍了. 2.然后要我下载了一个WinSCP用于windows和虚拟机之间的文件传输. 首先获得虚拟机的ip: 必须保持连接, 如果断开ip就是这样的 3.传输文件 将jd ...

  8. phpcms 列表页中调用其下的所有子栏目(或特定的子栏目)的方法

    phpcms 列表页中,如何调用其下的所有子栏目(或特定的子栏目),具体的写法如下,感兴趣的朋友可以参考下,希望对大家有所帮助 代码如下: {pc:get sql="select * fro ...

  9. 【BZOJ3790】神奇项链 Manacher+贪心

    [BZOJ3790]神奇项链 Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H ...

  10. zookeeper报错Will not attempt to authenticate using SASL (unknown error)

    Will not attempt to authenticate using SASL (unknown error) 转自:http://blog.csdn.net/mo_xingwang/arti ...