requests库详解
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库详解的更多相关文章
- python WEB接口自动化测试之requests库详解
由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...
- Python爬虫:requests 库详解,cookie操作与实战
原文 第三方库 requests是基于urllib编写的.比urllib库强大,非常适合爬虫的编写. 安装: pip install requests 简单的爬百度首页的例子: response.te ...
- requests库详解 --Python3
本文介绍了requests库的基本使用,希望对大家有所帮助. requests库官方文档:https://2.python-requests.org/en/master/ 一.请求: 1.GET请求 ...
- python接口自动化测试之requests库详解
前言 说到python发送HTTP请求进行接口自动化测试,脑子里第一个闪过的可能就是requests库了,当然python有很多模块可以发送HTTP请求,包括原生的模块http.client,urll ...
- 爬虫学习--Requests库详解 Day2
什么是Requests Requests是用python语言编写,基于urllib,采用Apache2 licensed开源协议的HTTP库,它比urllib更加方便,可以节约我们大量的工作,完全满足 ...
- Python爬虫学习==>第八章:Requests库详解
学习目的: request库比urllib库使用更加简洁,且更方便. 正式步骤 Step1:什么是requests requests是用Python语言编写,基于urllib,采用Apache2 Li ...
- python的requests库详解
快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...
- Python爬虫系列-Requests库详解
Requests基于urllib,比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求. 实例引入 import requests response = requests.get( ...
- Python之Unittest和Requests库详解
1.按类来执行 import unittest class f1(unittest.TestCase): def setUp(self): pass def tearDown(self): pass ...
随机推荐
- sql 注入风险
目录 sql 注入风险 什么是sql注入呢? 查看sql注入风险 如何避免 sql 注入风险 pymysql 简单规避注入风险示列 sql 注入风险 什么是sql注入呢? 参考百度 查看sql注入风险 ...
- hybris backoffice创建product遇到的synchronization问题和解答
我从product DSC-H20_MD clone了一个新的product,code为DSC-H20_MD1 因为它的状态有个红灯: 所以我点了这个sync按钮: 结果报这个错: 之后这个clone ...
- SAP 2019 TechEd Key Note解读:云时代下SAP从业人员如何做二次开发?
刚刚过去的在巴塞罗那举行的2019 SAP TechEd,SAP照例向全球广大的SAP生态圈从业者们传达了一些重要的信息,其中一条为:Building Extensions for the Intel ...
- java容器tomat,jboss支持内部读取软链接
tomcat,jboss允许软链接 作用:当war包解压过后,需要将内部进行链接到外部的可移动存储上时,就需要使用此功能. tomcat默认是不开启tomcat链接的,需要手动添加配置项. 在tomc ...
- PHP-5.6.8 源码包编译安装
一.下载源码包后,进行解压 [root@www home]# .tar.bz2 gzip: stdin: not in gzip format tar: Error is not recoverabl ...
- Windows性能计数器监控实践
Windows性能计数器(Performance Counter)是Windows提供的一种系统功能,它能实时采集.分析系统内的应用程序.服务.驱动程序等的性能数据,以此来分析系统的瓶颈.监控组件的表 ...
- redis 设置密码并运行外部连接
redis默认是不能远程访问的,如果希望多台机子共用redis数据库,那就需要开启redis远程连接访问.既然可以远程连接了,那就需要密码登陆,否则不安全.下面是具体的方法,按照步骤一步一步来就OK了 ...
- Spring4- 01 - Spring框架简介及官方压缩包目录介绍- Spring IoC 的概念 - Spring hello world环境搭建
一. Spring 框架简介及官方压缩包目录介绍 主要发明者:Rod Johnson 轮子理论推崇者: 2.1 轮子理论:不用重复发明轮子. 2.2 IT 行业:直接使用写好的代码. Spring 框 ...
- 倍增法求lca:暗的连锁
https://loj.ac/problem/10131 #include<bits/stdc++.h> using namespace std; struct node{ int to, ...
- Django之路——1 Django的简介
今天我们来学习django,在学习Django之前我们先来了解一下django和web开发中的http协议 1.mvc模型和mtv模型 既然学习Django,那么我们一定要知道web开发中的mvc模型 ...