python之_requests库学习_2(post请求参数传递)

本文探讨了如何使用Python的requests库进行定制请求头和复杂POST请求。内容包括通过字典传递表单数据,以元组形式传递多值参数,以及发送非表单格式的数据和多部分编码的文件,详细解释了data参数的用法,并展示了如何设置文件名、文件类型和请求头。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、定制请求头
定义headers,类型为dict

import requests
url = 'https://api.douban.com/v2/book/search?q=小王子'
headers={'user-agent': 'Mozilla/5.0'}
r = requests.get(url,headers=headers)

注:Requests 不会基于定制 header 的具体情况改变自己的行为。只不过在最后的请求中,所有的 header 信息都会被传递进去,如下图的请求头信息按照需求都可以这样子传递
这里写图片描述

二、更加复杂的POST请求
在上一篇文章中可以传递带参数的get请求,只需要在url后添加一个params参数即可,下面展示更加复杂的post请求
1、传递一些编码为表单形式的数据——非常像一个 HTML 表单。要实现这个,只需简单地传递一个字典给 data 参数。你的数据字典在发出请求时会自动编码为表单形式

import requests
payload={'key1':'value1','key2':'value2'} #参数为dict形式
r=requests.post("http://httpbin.org/post",data=payload)
print(r.url)
print(r.text)
>>http://httpbin.org/post #输出url
>>{
...
...
  "form": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    ...  ...
    }
}

data参数为元组形式,可传递一个key值包含多个值的参数

import requests
payload=(('key1','value1'),('key1','value2')) #参数为元组
r=requests.post("http://httpbin.org/post",data=payload)
print(r.url)
print(r.text)
>>http://httpbin.org/post
>>{
  ...  ...
  "form": {
    "key1": [
      "value1", 
      "value2"
    ]
  }, 
  "headers": {
    "Accept": "*/*", 
   ... ...
   }
}

2、很多时候你想要发送的数据并非编码为表单形式的。如果你传递一个 string 而不是一个 dict

import requests
import json
payload={'some':'data'}
url = 'http://httpbin.org/post'
r=requests.post(url,data=json.dumps(payload))
import requests
payload={'some':'data'}
url = 'http://httpbin.org/post'
r=requests.post(url,json=payload)
{
 ... ...
  "json": {
    "some": "data"
  }, 
... ...
}

3、post一个多部分编码的文件

import requests
url='http://httpbin.org/post'
path = r'E:\history.txt'
files={'file':open(path,'rb')}
r = requests.post(url,files=files) #参数为一个文件
print(r.url)
print(r.text)
>>http://httpbin.org/post
>>{
  ... ... 
  "files": {
    "file": "data:application/octet-stream;base64,gANjY29sbGVjdGlvbnMKZGVxdWUKcQBdcQEoSyJLOEtNS0JLRmVLBYZxAlJxAy4="
  }, 
  ... ...
  }

可以显示的设置文件名、文件类型和请求头

import requests
url='http://httpbin.org/post'
path = r'E:\history.txt'
files={'file':('report.xls',open(path,'rb'),'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url,files=files)

>>  "files": {
    "file": "data:application/vnd.ms-excel;base64,gANjY29sbGVjdGlvbnMKZGVxdWUKcQBdcQEoSyJLOEtNS0JLRmVLBYZxAlJxAy4="
  }, 

也可以发送作为文件来接收的字符串
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值