了解Requests库的使用

本文详细介绍了Python的Requests库,包括GET和POST请求的使用,如get请求的证书验证、post请求的表单提交和JSON提交,以及文件上传。同时,文章还展示了如何处理响应的HTTP状态码、响应头、文本和内容,以及如何解析JSON响应。
摘要由CSDN通过智能技术生成

包含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))

在这里插入图片描述
那个是https上传
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CY3761

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值