包含get、from-post、json-post,upload–post
# CY3761 | 2021-12-14 10:14
# 全方面的掌握Requests库的使用【python爬虫入门进阶】(02)
# https://feige.blog.csdn.net/article/details/120935824
import requests
from fake_useragent import UserAgent
import os
headers = {
'User-Agent': UserAgent().random
}
# 写一个 requests库的get请求
# 禁止证书验证 (处理不信任的SSL证书) verify
resp = requests.get('https://www.httpbin.org/get', {
'k': '这是中文'
}, headers=headers, verify=False)
# status_code HTTP状态码 (200表示请求成功) text 响应文本
if resp.status_code == 200:
resp.encoding = 'utf-8' # 设置响应文本的字符编码 返回HTML使用有效
print(resp.url, resp.request.method, resp.request.body)
print('-' * 80)
print('headers', type(resp.headers), sep='\n')
print(resp.headers) # 返回头对象
print('-' * 80)
print('text', type(resp.text), sep='\n')
print(resp.text) # 响应文本 (一般响应数据返回HTML使用)
print('-' * 80)
print('content', type(resp.content), sep='\n')
print(resp.content) # 响应b文本(二进制) (一般响应数据返回二进制使用 图片、视音频、压缩包等)
print('-' * 80)
print('json')
try:
print(type(resp.json()))
print(resp.json()) # 响应JSON (一般响应数据返回JSON使用, 如果非JSON会报错)
except (Exception, BaseException) as e:
print(e)
print('-' * 80)
print('from-post')
# 写一个requests库的post请求
# 以表单的方式提交 (默认POST)
resp = requests.post('https://www.httpbin.org/post', {
'k': '这是中文'
}, headers=headers)
if resp.status_code == 200:
resp.encoding = 'utf-8' # 设置响应文本的字符编码 返回HTML使用有效
print(resp.url, resp.request.method, resp.request.body)
print('-' * 80)
print('json')
try:
print(type(resp.json()))
print(resp.json()) # 响应JSON (一般响应数据返回JSON使用, 如果非JSON会报错)
except (Exception, BaseException) as e:
print(e)
print('-' * 80)
print('json-post')
# JSON提交 头信息需要 'content-type':'application/json;charset=utf-8'
jsonHeaders = headers
jsonHeaders['content-type'] = 'application/json;charset=utf-8'
resp = requests.post('https://www.httpbin.org/post', json={
'k': '这是中文'
}, headers=jsonHeaders)
if resp.status_code == 200:
resp.encoding = 'utf-8' # 设置响应文本的字符编码 返回HTML使用有效
print(resp.url, resp.request.method, resp.request.body)
print('-' * 80)
print('json')
try:
print(type(resp.json()))
print(resp.json()) # 响应JSON (一般响应数据返回JSON使用, 如果非JSON会报错)
except (Exception, BaseException) as e:
print(e)
# 利用requests库如何实现上传请求
print('-' * 80)
print('upload-post')
# 需要看另一篇 https://blog.csdn.net/u014534808/article/details/113665128
from requests_toolbelt.multipart.encoder import MultipartEncoder # pip install requests-toolbelt (文件上传才会用)
# 图床地址
uploadUrl = 'https://imgurl.org/upload/ftp'
uploadFilePath = '8.jpg'
uploadData = MultipartEncoder(
fields={
'file': (os.path.basename(uploadFilePath), open(uploadFilePath, 'rb'), 'application/octet-stream'),
}
)
uploadHeaders = headers
uploadHeaders['Content-Type'] = uploadData.content_type
resp = requests.post(uploadUrl, data=uploadData, headers=uploadHeaders)
if resp.status_code == 200:
resp.encoding = 'utf-8' # 设置响应文本的字符编码 返回HTML使用有效
print(resp.url, resp.request.method, resp.request.body)
print(resp.text)
# 题外测试 关键字参数注解
print('-' * 80)
items = dict(name='小明', age=18)
print('我叫{name}, 今年{age}, 我真的叫{name}, 今年真的是{age}'.format(**items))