09-requests-安装及入门
Posted 爱学习de测试小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了09-requests-安装及入门相关的知识,希望对你有一定的参考价值。
目录
安装
- pip install requests
快速上手
发送请求
# -*- coding: utf-8 -*-
# @Time : 2021/5/5
# @Author : 大海
import requests
# 发送get请求
r = requests.get('https://api.github.com/events')
print(r)
# 发送post请求
r = requests.post('http://httpbin.org/post', data={'key': 'value'})
print(r)
# 发送PUT,DELETE,HEAD 以及 OPTIONS请求
r1 = requests.put('http://httpbin.org/put', data={'key': 'value'})
r2 = requests.delete('http://httpbin.org/delete')
r3 = requests.head('http://httpbin.org/get')
r4 = requests.options('http://httpbin.org/get')
print(r1, r2, r3, r4)
传递URL参数
# -*- coding: utf-8 -*-
# @Time : 2021/5/5
# @Author : 大海
import requests
# 例如, http://httpbin.org/get?key=val&key2=value2
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
print(r.url)
# 列表作为值传入
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
r = requests.get('http://httpbin.org/get', params=payload)
print(r.url)
响应内容
# -*- coding: utf-8 -*-
# @Time : 2021/5/5
# @Author : 大海
import requests
r = requests.get('https://api.github.com/events')
# 文本格式
print(r.text)
print(type(r.text))
# 二进制格式
print(r.content)
print(type(r.content))
# json格式
print(r.json())
print(type(r.json()))
# 原始响应内容
print(r.raw)
print(type(r.raw))
添加请求头
# 添加请求头,只要简单地传递一个 dict 给 headers 参数就可以了
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)
发送post请求
# -*- coding: utf-8 -*-
# @Time : 2021/5/5
# @Author : 大海
import json
import requests
# 发送post请求
# 1.表单形式的数据,只需简单地传递一个字典给 data 参数
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print(r.text)
# 还可以为 data 参数传入一个元组列表。在表单中多个元素使用同一 key 的时候,这种方式尤其有效
payload = (('key1', 'value1'), ('key1', 'value2'))
r = requests.post('http://httpbin.org/post', data=payload)
print(r.text)
# 2.请求体-json格式数据
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))
# 使用 json 参数直接传递
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
r = requests.post(url, json=payload)
# 3.上传文件
url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)
print(r.text)
# 显式地设置文件名,文件类型和请求头
url = 'http://httpbin.org/post'
files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url, files=files)
print(r.text)
响应信息
# -*- coding: utf-8 -*-
# @Time : 2021/5/5
# @Author : 大海
import requests
r = requests.get('http://httpbin.org/get')
# 响应状态码
print(r.status_code)
# 响应头
print(r.headers)
print(r.headers['Content-Type']) # 获取响应头某些字段值
print(r.headers.get('content-type'))
Cookie
# -*- coding: utf-8 -*-
# @Time : 2021/5/5
# @Author : 大海
import requests
# 添加cookies
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
print(r.text)
# 获取cookies
# print(r.cookies.get('cookie_name')) # 示例,运行会报错
# Cookie 的返回对象为 RequestsCookieJar,它的行为和字典类似,但接口更为完整,适合跨域名跨路径使用
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
url = 'http://httpbin.org/cookies'
r = requests.get(url, cookies=jar)
print(r.text)
超时与重定向
# -*- coding: utf-8 -*-
# @Time : 2021/5/5
# @Author : 大海
import requests
r = requests.get('http://github.com')
# 请求url
print(r.url)
# 请求历史
print(r.history)
# 重定向设置allow_redirects True 允许,False 不允许
r = requests.get('http://github.com', allow_redirects=False)
print(r.status_code)
print(r.history)
# 超时设置,timeout 单位秒
r = requests.get('http://github.com', timeout=0.5)
print(r.status_code)
以上是关于09-requests-安装及入门的主要内容,如果未能解决你的问题,请参考以下文章