Python学习笔记(五) requets多种请求参数

192 阅读1分钟

Python学习笔记(五) requets多种请求参数

1. application/x-www-form-urlencoded数据格式


  url = 'http://api.newibao.com/web/essay/publicEssayList'
  param = {
    'page':1,
    'size':64
  }
  headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
    'Accept-Encoding': 'gzip, deflate'
  }
  
  req = request.get(headers=headers,url=url,params=param)
  req.encoding ='utf-8' # 不是必要
  text = req.json()
  print(text)

2. application/json数据格式

self.target ='https://www.logosc.cn/api/searchIconsByKeywords' 
self.server = 'https://www.logosc.cn/'
param = {"page": 2, "keywords": "团队", "type": "", "tag_id": 0}
self.param = json.dumps(param)


self.headers ={
    'accept': "application/json, text/plain, */*",
    'accept-encoding': 'gzip, deflate, br',
    'authorization': 'Bearer',
    'content-type': 'application/json;charset=UTF-8',
    'cookie': 'Hm_lvt_afb69c6fd73381e3567a9cf3690b9aaa=1600064493; XSRF-TOKEN=eyJpdiI6ImdQMFJ2eWk1cCtwNEZGQlRVdkxOOHc9PSIsInZhbHVlIjoidHgwcEF2eElndmZSK2FBc3JGVVwvalMxT1pPZzlcL3hSbFdvVmI2VjhGcGErMzl6RWUzK3BXSk9mTnZROGVsdXpLUUxPTklqZHZLUjZKWXdpXC9vWU9DeEE9PSIsIm1hYyI6IjliMTNlMGQ3N2U0MGNmYjljZDI5NTg1NWFlM2IxNzRkNDJlZDYxNDg3OTVkMmNkZmI4N2FjMzBhZGJiNWFiNzAifQ%3D%3D; laravel_session=eyJpdiI6IlRzNTVTMFRjVGJMSWZnSVhsQlVZTmc9PSIsInZhbHVlIjoiQ3JVVE9ZRlZZSTJRdkI5TFBVQkxiZDJBSTNnZUZNZ3c0NFBQWkVTY215NVBWZ0pjazZZekNBWDVXYW1yYmMyTmY4dE9ZR0VxMmxvVHlFMEFFWVNyZEE9PSIsIm1hYyI6IjM0MjZjNDA4ZjQyZjVhYTdlNmQ4YWI0ZWQzNDc3ZTBhNGM2NWRlYjVkZDRlMzY4OWUwMjI0NmYwNGQyYmE2YzMifQ%3D%3D; Hm_lpvt_afb69c6fd73381e3567a9cf3690b9aaa=1600065090',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
}

req = requests.post(url=self.url,data=self.param,headers=self.headers)

3. text/xml数据格式

1. xml字符串

xml = """my xml"""
headers = {'Content-Type': 'application/xml'}
requests.post('http://www.example.com', data=xml, headers=headers)

2. 读取xml文件

import requests

def request_ws(request):
with open(archivo_request,"r") as archivo:
    request_data = archivo.read()

target_url = "http://127.0.0.1:8000/?wsdl"

headers = {'Content-type':'text/xml'}

data_response = requests.post(target_url, data=request_data, headers=headers)

4 multipart/form-data数据格式

表单提交

import requests
import json
# 设置URL
url = "http://demo.9meikf.cn/usystem/auto/getAnswer.do"
# 设置消息头
headers = {
    "Cookie":"JSESSIONID=EA01FF2B025861F39E29712C97F7DF69;CASTGC=TGT-136-bLQMf0CAikK4BGaydOfIeKd6tWpZQEznJ2ZWdcVl9ofI4LiaQb-cas01.example.org",
    "Content-Type":"application/json"
    }
# 设置消息体
data = {"companyId":"48622",
        "nodeId":6,
        "question":"不需要",
        "templateId":"c6f5ad67fc2c11e8a11800163e086942"}
# 获取相应
response=requests.post(url,headers=headers,data=json.dumps(data))
print("Status code:",response.status_code)
print(response.text)
# 解析相应
info=response.json()
# 验证数据
assert str(info['answer'])=='reject'

文件上传

import requests 
files = {"file": open("C:/Users/Administrator/Desktop/test.txt", "rb")}
r = requests.post("http://httpbin.org/post", files=files) 
print(r.text)

本文使用 mdnice 排版