学习目的:

  request库比urllib库使用更加简洁,且更方便。

正式步骤

Step1:什么是requests


  requests是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库。它比urllib更加方便,可以节约大量工作时间,还完全满足HTTP测试需求,是一个简单易用的HTTP库。

Step2:实例 引入


  

# -*-  coding:utf-8 -*-

import requests

response = requests.get('http://www.baidu.com')
print(type(response))
print(response.content)
print(response.status_code)
print(response.text)
print(type(response.text))
print(response.cookies)

重要:

  • response.content():这是从网络上直接抓取的数据,没有经过任何的解码,是一个bytes类型,
  • response.text():这是str类型数据,是requests库将response.content进行解码的字符串,解码需要指定一个编码方式,requests会根据自己的猜测来判断编码方式,有时候会判断错误,所以最稳妥的办法是response.content.decode("utf-8"),指定一个编码方式手动解码

Step3:各种请求方式


  

# -*-  coding:utf-8 -*-
import requests requests.post('http://httpbin.org/post')
requests.put('http://httpbin.org/put')
requests.delete('http://httpbin.org/delete')
requests.head('http://httpbin.org/get')
requests.options('http://httpbin.org/get')
  1. get请求
    ① 基本用法

    # -*-  coding:utf-8 -*-
    
    import requests
    
    response = requests.get('http://httpbin.org/get')
    print(response.text)

    运行结果:

    {
    "args": {},
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.18.4"
    },
    "origin": "222.94.50.178",
    "url": "http://httpbin.org/get"
    }

    ②带参数的get请求

    import requests
    
    data = {
    'name':'python','age':17
    } response = requests.get('http://httpbin.org/get',params=data)
    print(response.text)

    运行结果:

    {
    "args": {
    "age": "",
    "name": "python"
    },
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.18.4"
    },
    "origin": "222.94.50.178",
    "url": "http://httpbin.org/get?name=python&age=17"
    }

    get和post请求的区别:

    • GET是从服务器上获取数据,POST是向服务器传送数据

    • GET请求参数显示,都显示在浏览器网址上,HTTP服务器根据该请求所包含URL中的参数来产生响应内容,即“Get”请求的参数是URL的一部分。 例如: http://www.baidu.com/s?wd=Chinese

    • POST请求参数在请求体当中,消息长度没有限制而且以隐式的方式进行发送,通常用来向HTTP服务器提交量比较大的数据(比如请求中包含许多参数或者文件上传操作等),请求的参数包含在“Content-Type”消息头里,指明该消息体的媒体类型和编码,
      注意:避免使用Get方式提交表单,因为有可能会导致安全问题。 比如说在登陆表单中用Get方式,用户输入的用户名和密码将在地址栏中暴露无遗。

    ③解析Json

    import requests
    import json response = requests.get('http://httpbin.org/get')
    print(response.json())
    print(type(response.json()))

    ④获取二进制数据

    # -*-  coding:utf-8 -*-
    '''
    保存百度图标
    '''
    import requests response = requests.get('https://www.baidu.com/img/bd_logo1.png')
    with open('baidu.png','wb') as f:
    f.write(response.content)
    f.close()

    ⑤添加headers
    如果直接爬取知乎的网站,是会报错的,如:

    import requests
    
    response = requests.get('https://www.zhihu.com/explore')
    print(response.text)

    运行结果:

    <html><body><h1>500 Server Error</h1>
    An internal server error occured.
    </body></html>

    解决办法:

    import requests
    headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    }
    response = requests.get('https://www.zhihu.com/explore',headers = headers)
    print(response.text)

    就是添加一个headers,就可以正常抓取,而headers中的数据,我是通过chrome浏览器自带的开发者工具去找了然后copy过来的

  2. 基本POST请求
    import requests
    
    data = {
    'name':'python','age' : 18
    }
    headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    } response = requests.post('http://httpbin.org/post',data=data,headers=headers)
    print(response.json())

    实例:爬取拉勾网python职位,并把数据保存为字典

    # -*-  coding:utf-8 -*-
    
    import requests
    
    headers = {
    'User-Agent' :'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
    'Chrome/69.0.3497.100 Safari/537.36',
    'Referer':"https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput="
    } data = {
    'first':"True",
    'pn':"",
    'kd' :"python"
    }
    url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false' response = requests.get(url,headers=headers,params=data)
    print(response.json())
  3. 响应
    import requests
    '''
    response属性
    '''
    response = requests.get('http://www.baidu.com')
    print(response.status_code,type(response.status_code))
    print(response.history,type(response.history))
    print(response.cookies,type(response.cookies))
    print(response.url,type(response.url))
    print(response.headers,type(response.headers))

    运行结果:

    200 <class 'int'>
    [] <class 'list'>
    <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]> <class 'requests.cookies.RequestsCookieJar'>
    http://www.baidu.com/ <class 'str'>
    {'Server': 'bfe/1.0.8.18', 'Date': 'Thu, 05 Apr 2018 06:27:33 GMT', 'Content-Type': 'text/html', 'Last-Modified': 'Mon, 23 Jan 2017 13:28:24 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'Keep-Alive', 'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Pragma': 'no-cache', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Content-Encoding': 'gzip'} <class 'requests.structures.CaseInsensitiveDict'>
  4. 状态码判断
    状态码参考表 http://www.cnblogs.com/wuzhiming/p/8722422.html
    # -*-  coding:utf-8 -*-
    
    import requests
    
    response = requests.get('http://www.cnblogs.com/hello.html')
    exit() if not response.status_code == requests.codes.not_found else print('404 not found') response1 = requests.get('http://www.baidu.com')
    exit() if not response1.status_code == requests.codes.ok else print('Request Successly')
  5. 高级操作
    ①文件上传
    import requests
    
    file = {'file':open('baidu.png','rb')}
    response = requests.post('http://httpbin.org/post',files = file)
    print(response.text)

    运行结果不演示

    ②获取cookie

    import requests
    
    response = requests.get('http://www.baidu.com')
    cookies = response.cookies
    print(cookies)
    for key,value in cookies.items():
    print(key + '=' + value)

    ③会话维持

    import requests
    
    s = requests.Session()
    s.get('http://httpbin.org/cookies/get/number/123456789')
    response = s.get('http://httpbin.org/cookies')
    print(response.text)

    ④证书验证

    import requests
    
    #verify=False表示不进行证书验证
    response = requests.get('https://www.12306.cn',verify=False)
    print(response.status_code)

    手动指定证书

    response1 = requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key'))

    ⑤代理设置

    import requests
    #用法示例,代理可以自己百度免费的代理
    proxies = {
    'http':'http://127.0.0.1:端口号',
    'https':'https://ip:端口号',
    'http':'http://username:password@ip:端口号'
    } response = requests.get('http://www.baidu.com',proxies=proxies)
    print(response.status_code)

    ⑥超时设置

    import requests
    
    response = requests.get('http://httpbin.org/get',timeout = 1)
    print(response.status_code)

    ⑦认证设置

    import requests
    from requests.auth import HTTPBasicAuth response = requests.get('http://127.0.0.1:8888',auth=('user','password'))
    response1 = requests.get('http://127.0.0.1:8888',auth=HTTPBasicAuth('user','passwrd'))
    print(response.status_code)

    PS:127.0.0.1:8888只是举例

    ⑧异常处理

    import requests
    from requests.exceptions import ReadTimeout,HTTPError,RequestException try:
    response = requests.get('http://httpbin.org/get',timeout = 0.01)
    print(response.status_code)
    except ReadTimeout:
    print("TIME OUT")
    except HTTPError:
    print('HTTP ERROR')
    except RequestException:
    print("ERROR")

学习总结:


  通过爬虫的学习可以进一步的掌握python的基础应用,我的目的就是这个,后面继续学习

Python爬虫学习==>第八章:Requests库详解的更多相关文章

  1. python WEB接口自动化测试之requests库详解

    由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...

  2. Python爬虫学习笔记-2.Requests库

    Requests是Python的一个优雅而简单的HTTP库,它比Pyhton内置的urllib库,更加强大. 0X01 基本使用 安装 Requests,只要在你的终端中运行这个简单命令即可: pip ...

  3. python爬虫学习,使用requests库来实现模拟登录4399小游戏网站。

    1.首先分析请求,打开4399网站. 右键检查元素或者F12打开开发者工具.然后找到network选项, 这里最好勾选perserve log 选项,用来保存请求日志.这时我们来先用我们的账号密码登陆 ...

  4. Python爬虫利器一之Requests库的用法

    前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...

  5. python爬虫学习(6) —— 神器 Requests

    Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 H ...

  6. (转)Python爬虫利器一之Requests库的用法

    官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...

  7. Python爬虫:requests 库详解,cookie操作与实战

    原文 第三方库 requests是基于urllib编写的.比urllib库强大,非常适合爬虫的编写. 安装: pip install requests 简单的爬百度首页的例子: response.te ...

  8. 爬虫学习--Requests库详解 Day2

    什么是Requests Requests是用python语言编写,基于urllib,采用Apache2 licensed开源协议的HTTP库,它比urllib更加方便,可以节约我们大量的工作,完全满足 ...

  9. python爬虫学习(一):BeautifulSoup库基础及一般元素提取方法

    最近在看爬虫相关的东西,一方面是兴趣,另一方面也是借学习爬虫练习python的使用,推荐一个很好的入门教程:中国大学MOOC的<python网络爬虫与信息提取>,是由北京理工的副教授嵩天老 ...

随机推荐

  1. 第六章 组件 56 组件-组件中的data

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  2. Java-Dom4jHelper工具类

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...

  3. Java-ConfigHelper工具类

    /** * 读取配置文件 */ import java.io.File; import java.net.URL; import org.apache.commons.configuration.Co ...

  4. vue2.0 + element-ui2实现分页

    当我们向服务端请求大量数据的时候,并要在页面展示出来,怎么办?这个时候一定会用到分页. 本次所使用的是vue2.0+element-ui2.12实现一个分页功能,element-ui这个组件特别丰富, ...

  5. .net大文件传输断点续传源码

    IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...

  6. 灰度图像--频域滤波 傅里叶变换之离散傅里叶变换(DFT)

    学习DIP第23天 转载请标明本文出处:http://blog.csdn.net/tonyshengtan,欢迎大家转载,发现博客被某些论坛转载后,图像无法正常显示,无法正常表达本人观点,对此表示很不 ...

  7. HFUUOJ1023 闷声发大财 概率dp

    题意 xyq有\(n\)个骰子,第\(i\)个骰子有\(a_i\)面,每次xyq都会把\(n\)个骰子搞一遍,其中的最小值作为结果,问最终结果的期望\(\mod (10^9+7 )\). 分析 lfx ...

  8. curl: (35) Server aborted the SSL handshake 解决办法

    # 先删除curl brew uninstall curl # 重新安装curl,带上--with-openssl brew install curl --with-openssl # 然后重启下ph ...

  9. git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题

    在同步本地文件到线上仓库的时候 报错 pre -commit hook failed (add --no-verify to bypass) 当你在终端输入git commit -m "xx ...

  10. vue 使用axios 出现跨域请求的两种解决方法

    最近在使用vue axios发送请求,结果出现跨域问题,网上查了好多,发现有好几种结局方案. 1:服务器端设置跨域 header(“Access-Control-Allow-Origin:*”); h ...