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. springMVC + mybatis 下出现JDBC Connection *** will not be managed by Spring错误

    仔细查看配置中是否有如下类似的配置 execution(* com.ciguo.service.*.*(..)) <aop:config> <aop:pointcut id=&quo ...

  2. 信号的有效值(RMS)估计

    % Root Mean Square Value function [retval] = rms1(sig) N = 20; for k = 1 : length(sig)/N - 1 sig_sum ...

  3. SAP NOTE 1999997 - FAQ: SAP HANA Memory

    Symptom You have questions related to the SAP HANA memory. You experience a high memory utilization ...

  4. Android笔记(六十七) 自定义控件

    实际编程中,系统提供的控件往往无法满足我们的需求,一来是样子丑陋,二来是一些复杂的组合需要多次使用的话,每次都写一堆控件的组合会很耗费时间,所以我们将这些组件的组合自定义为一个新的控件,以后使用的时候 ...

  5. jeecg的开发api接口之旅(http)

    一.接口测试工具 1.postman下载地址:https://download.csdn.net/download/qq_35792159/11898005 2.谷歌浏览器插件:https://www ...

  6. react navtagion 头部有返回按钮 标题不居中解决方法

    头部右边写一个隐藏的组件 hederRight:( <View><View> )

  7. InvenSense 美国公司

    InvenSense为智能型运动处理方案的先驱.全球业界的领导厂商,驱动了运动感测人机接口在消费性电子产品上的应用.公司提供的集成电路(IC)整合了运动传感器-陀螺仪以及相对应的软件,有别于其他厂商, ...

  8. SVM: 实际中使用SVM的一些问题

    使用SVM包来求θ,选择C与核函数 我们使用已经编写好的软件包(这些软件包效率高,用得多,是经无数人证明已经很好的可以使用的软件包)来求θ,而不是自己去编写软件来求它们(就像我们现在很少编写软件来求x ...

  9. 四.Protobuf3 缺省值

    解析消息时,如果编码消息不包含特定的单数元素,则解析对象中的相应字段将设置为该字段的默认值.这些默认值是特定于类型的: 对于字符串,默认值为空字符串. 对于字节,默认值为空字节. 对于布尔,默认值为f ...

  10. Dubbo官方文档

    官方文档:http://dubbo.apache.org/en-us/docs/user/quick-start.html