python http 请求 响应 post表单提交

1. 查看请求 响应情况

print(response.text)

print(response.headers)

print(response.request.body)

print(response.request.headers)

2. post的multipart/form-data请求

# multipart/form-data请求

url = 'https://www.douban.com/group/topic/111306566/add_comment#last'
rv_comment = " 自动帮你顶帖 "
verify_code = ''
pic_id = ''
payload = {
"ck": (None,cookie.get('ck')),
"rv_comment": (None,rv_comment),
"start": (None,0),
# "captcha-solution": verify_code,
# "captcha-id": pic_id,
"submit_btn": (None,"发送")
}
cookiesDit = cookie.getAll()
headers['Accept'] = 'text/plain, */*; q=0.01'
headers['Accept-Encoding'] = 'gzip, deflate, br'
headers['Accept-Language'] = 'zh-CN,zh;q=0.9'
response = requests.post(url, files=payload , headers=headers, cookies=cookiesDit)

# https://blog.csdn.net/yu12377/article/details/77188895

转:https://www.cnblogs.com/LanTianYou/p/8379419.html

要模拟multipart/form-data类型请求,可以用python3的requests库完成。代码示例如下: 

#请求的接口url
url = "url"

#假设待上传文件与脚本在同一目录下
dir_path = os.path.abspath(os.path.dirname(__file__))
#待上传文件的路径,这里假设文件名为test.txt
file_path = os.path.join(dir_path,'test.txt')

'''
    field_1,field_2...field_n代表普通字段名
    value_1,value_2...value_n代表普通字段值
    files为文件类型字段

    实际应用中字段名field_n均需要替换为接口抓包得到的实际字段名
    对应的字段值value_n也需要替换为接口抓包得到的实际字段值
'''
files={
    'field_1':(None,'value_1'),
    'field_2':(None,'value_2'),
    ...
    'field_n':(None,'value_n'),
    'files':('test.txt',open(file_path,'rb'),'text/plain'),
}

r = s.post(url,files=files)
原文地址:https://www.cnblogs.com/swing07/p/9371493.html