import requests

 #实例引入
# response = requests.get('http://www.baidu.com')
# print(type(response))
# print(response.status_code)
# print(type(response.text))
# print(response.text)
# print(response.cookies)
#
# #各种请求方式
# a = requests.post('http://httpbin.org/post')
# b = requests.put('http://httpbin.org/put')
# requests.delete('http://httpbin.org/delete')
# requests.head('http://httpbin.org/get')
# requests.options('http://httpbin.org/get')
# print(a.text) #请求
#基本GET请求
#基本写法
# response = requests.get('http://httpbin.org/get')
# print(response.text)
# #带参数GET请求
# response = requests.get('http://httpbin.org/get?name=gemmey&age=22')
# print(response.text)
#
# data = {
# 'name':'gemmey',
# 'age':22
# }
# response = requests.get('http://httpbin.org/get',params=data)
# print(response.text)
#
# #解析Json
# import json
# response = requests.get('http://httpbin.org/get')
# print(type(response.text))
# print(response.json())#执行了json.loads()操作
# print(json.loads(response.text))
# print(type(response.json())) #获取二进制数据(图片、视频等)
# response = requests.get('http://github.com/favicon.ico')
# print(type(response.text),type(response.content))
# print(response.text)
# print(response.content)
# with open('favicon.ico','wb') as f:
# f.write(response.content) #添加Headers
#不加headers会被屏蔽
# headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
# response = requests.get('https://www.zhihu.com/explore',headers=headers)
# #response.encoding = 'utf-8'
# print(response.text) #基本POST请求 #form表单提交
# data = {'name':'germey','age':22}
# response = requests.post('http://httpbin.org/post',data=data)
# print(response.text)
# #加个headers
# data = {'name':'germey','age':22}
# headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
# response = requests.post('http://httpbin.org/post',data=data,headers=headers)
# print(response.json()) #响应 #response的属性
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
# response = requests.get('http://www.jianshu.com',headers=headers)
# print(type(response.status_code),response.status_code)
# print(type(response.headers),response.headers)
# print(type(response.cookies),response.cookies)
# print(type(response.url),response.url)
# print(type(response.history),response.history) #状态码判断
# response = requests.get('http://www.jianshu.com',headers = headers)
# exit() if not response.status_code==requests.codes.ok else print('Request Successfilly')
# response = requests.get('http://www.jianshu.com',headers = headers)
# exit() if not response.status_code==200 else print('Request Successfilly')
#
# #高级操作
# #文件上传
# files = {'file':open('favicon.ico','rb')}
# response = requests.post('http://httpbin.org/post',files=files)
# print(response.text) #获取cookie
# response = requests.get('http://baidu.com')
# print(response.cookies)
# for key,value in response.cookies.items():
# print(key + '=' + value) #会话维持
#模拟登录
# requests.get('http://httpbin.org/cookies/set/number/123456789')
# response = requests.get('http://httpbin.org/cookies')
# print(response.text)
# '''
# {
# "cookies": {}
# }
# 相当于用两个浏览器分别访问
# '''
# #实例化Session对象,相当于一个浏览器访问
# s = requests.Session()
# s.get('http://httpbin.org/cookies/set/number/123456789')
# response = s.get('http://httpbin.org/cookies')
# print(response.text) #证书验证
#SSL提示的错误,访问HTTPS它首先验证证书,如果证书不合法会报错;是否进行证书验证verify
# from requests.packages import urllib3
# urllib3.disable_warnings()#消除警告信息
# response = requests.get('https://www.12306.cn',verify=False)
# print(response.status_code) #手动指定证书
#本地没有证书
# response = requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key'))
# print(response.status_code) #代理设置
#我这两个代理失效了
# proxies = {
# 'http':'http://127.0.0.1:9743',
# 'https':'https://127.0.0.1:9743',
# }
# response = requests.get('https://www.taobao.com',proxies=proxies)
# print(response.status_code)
#代理有用户名和密码
# proxies = {
# 'http':'http://user:password@127.0.0.1:9743/',
# }
# response = requests.get('https://www.taobao.com',proxies=proxies)
# print(response.status_code) #超时设置
# from requests.exceptions import ReadTimeout
# try:
# response = requests.get('https://httpbin.org/get',timeout=0.5)
# print(response.status_code)
# except ReadTimeout:
# print('TimeOut') #认证设置,需要用户名密码
# from requests.auth import HTTPBasicAuth
#
# r = requests.get('http://120.27.34.24:9001',auth=HTTPBasicAuth('user','123'))
# print(r.status_code) #异常处理
from requests.exceptions import ReadTimeout,HTTPError,RequestException,ConnectionError try:
response = requests.get('http://httpbin.org/get',timeout=0.1)
print(response.status_code)
except ReadTimeout:
print('Timeout')
except ConnectionError:
print('Connect error')
except HTTPError:
print('Http error')
except RequestException:
print('Error')

requests库详解的更多相关文章

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

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

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

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

  3. requests库详解 --Python3

    本文介绍了requests库的基本使用,希望对大家有所帮助. requests库官方文档:https://2.python-requests.org/en/master/ 一.请求: 1.GET请求 ...

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

    前言 说到python发送HTTP请求进行接口自动化测试,脑子里第一个闪过的可能就是requests库了,当然python有很多模块可以发送HTTP请求,包括原生的模块http.client,urll ...

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

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

  6. Python爬虫学习==>第八章:Requests库详解

    学习目的: request库比urllib库使用更加简洁,且更方便. 正式步骤 Step1:什么是requests requests是用Python语言编写,基于urllib,采用Apache2 Li ...

  7. python的requests库详解

    快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...

  8. Python爬虫系列-Requests库详解

    Requests基于urllib,比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求. 实例引入 import requests response = requests.get( ...

  9. Python之Unittest和Requests库详解

    1.按类来执行 import unittest class f1(unittest.TestCase): def setUp(self): pass def tearDown(self): pass ...

随机推荐

  1. HTML Marquee跑马灯

    Marquee是html的标签,所有的主流浏览器都能兼容,用于创建文字滚动. 来介绍下标签的属性 滚动方向 direction <!--滚动方向 direction 4个值 up down le ...

  2. RedisCluster的rename机制失败报错,解决又是数据倾斜问题

    需求说明:spring session中的用户session更新是更新key的名字,所以对于key的操作时需要用newkey 替换oldkey value值只允许存在一个,这里用到rename就很合适 ...

  3. 批量改主机名报错:Address 192.168.43.117 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

    ssh连接批量修改主机名报出以下提示: [root@bqh-nfs- ~]# vim modfilyhostname.sh [root@bqh-nfs- ~]# sh modfilyhostname. ...

  4. p3.BTC-协议

    数字货币是文件,难伪造,但是容易复制,不像实体货币,花出去就没了,数字货币存在double spending attack,双花攻击. 去中心化的货币,需要解决两个问题: 1.货币的发行 挖矿 2.交 ...

  5. Linux磁盘管理——MBR 与 GPT

    硬件设备在Linux中文件名 如今IDE 磁盘几乎被淘汰,市面上最常见的磁盘就是SATA和SAS.个人计算机主要是SATA.很多Linux发行版下都将IDE磁盘文件名也都被仿真成 /dev/sd[a- ...

  6. prometheus 告警规则

    GitHub网址1 https://github.com/samber/awesome-prometheus-alerts 网址2 https://awesome-prometheus-alerts. ...

  7. 从零开始配置MacBook Pro

    购买macbook, 是因为它的效率性.由于我第一次使用macbook,所以按照我开发的习惯和参照了其他人的文章进行配置我的个人mac,希望我的设置对你们也有所帮助 1.基本配置 查找我的Mac 系统 ...

  8. python高级特性-迭代器

    凡是可作用于for循环的对象都是Iterable类型: 凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列: 集合数据类型如list.dict.str等是Itera ...

  9. selenium常用的API(一)截屏

    我们在使用selenium测试过程中,可使用截屏功能将用例执行失败的画面截图保存,方便测试执行结束后查看并定位问题. 以下介绍两种截屏方法: 对当前浏览器窗口截屏 使用selenium自带的get_s ...

  10. 重装windows10系统的教程

    1.首先从官网上下载一个win10的系统, 2.准备一个8GB的移动U盘,下载好的镜像文件烧录在这个U盘, 3.按照不同型号的机型,使用不同的按键进入BOIS模式,然后选中U盘作为启动项,读取出来这个 ...