python 爬虫怎么构造 提交的类型为 multipart/form-data 的数据

post请求提交的数据格式为下图,怎么

图片说明

图片说明

直接用字符串数据拼接

利用python的poster模块。
安装poster:pip install poster

from poster.encode import multipart_encode 
from poster.streaminghttp import register_openers 
url = "http://www.example.com"
body_value = {"package": "com.tencent.lian","version_code": "66" }
register_openers()
datagen, re_headers = multipart_encode(body_value)
request = urllib2.Request(url, datagen, re_headers)
# 如果有请求头数据,则添加请求头
request .add_header(keys, headers[keys])
result = urllib2.urlopen(request ).read()

设置content-type,, 在请求的求情头里面设置,,,

import requests # 导入requests模块
headers = {
'Content-Type': 'application/x-www-form-urlencoded' # 设置请求头接收数据类型
}
files = {
'files': 'files' # 需要上传的文件数据
}
url = '' #api接口
requests.get(url, headers=headers, files=files)

post中的提交表单 ,之间加入字典就行,各个参数值有各种要求,要看具体情况。有些反爬虫机制就在这里体现,具体可以看我博客的一些文章,或者联系我,希望对你有用