在写爬虫有时候会发现这种请求数据,其实就是一个上传图片的请求。 def send_file(user,user_key,tousername,file,cookice): ''' 发送文件表情之类 :param user: 发送消息的用户 [{'FromUserName':FromUserName,'Content':Content}] :param user_key: 当前登录用户的凭证信息 :param tousername: :param file: 文件地址 :param cookice: :return: ''' filename = os.path.basename(file) stime = time.asctime( time.localtime(time.time())).split(' ') stime.insert(4,stime[-1]) stime[-1] = 'GMT+0800' stime = ' '.join(stime) m1 = hashlib.md5() with open(file,'rb') as f: m1.update(f.read()) md5 = m1.hexdigest() size = os.path.getsize(file) json_data = { "BaseRequest": { "Uin": user_key['wxuin'], "Sid": user_key['wxsid'], "Skey": user_key['skey'], "DeviceID": "e823192381686302" }, "ClientMediaId": int(time.time() * 1000), "TotalLen": size, "StartPos": 0, "DataLen": size, "MediaType": 4, "FromUserName": user['UserName'], "ToUserName": tousername, "FileMd5": md5 } file_headers_1 = { 'id':(None,'WU_FILE_0'), 'name':(None,filename), 'type':(None,'image/gif'), 'lastModifiedDate':(None,stime), 'size':(None,str(size)), 'mediatype':(None,'doc'), 'uploadmediarequest':(None,json.dumps(json_data,ensure_ascii=False)), 'webwx_data_ticket':(None,cookice.get('webwx_data_ticket')), 'pass_ticket':(None,user_key['pass_ticket']), 'filename':(filename, open(file, 'rb'),'image/gif'), 'Content-Type':'image/gif', } multipar_encoder = MultipartEncoder(fields=file_headers_1,boundary='---------------------------22619409317427') url = 'https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json' headers ={ 'Host': 'file.wx2.qq.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0', 'Accept': '*/*', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Accept-Encoding': 'gzip, deflate, br', 'Referer': 'https://wx2.qq.com/', 'Content-Type': multipar_encoder.content_type, 'Origin': 'https://wx2.qq.com', } res = requests.post(url,cookies=cookice,data=multipar_encoder,headers=headers) mediaid = res.json().get('MediaId') baseurl = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendemoticon?fun=sys&lang=zh_CN&pass_ticket=pass_ticket={0}' sendurl = baseurl.format(user_key['pass_ticket']) ctime = int(time.time() * 10000000) send_data = {"BaseRequest": {"Uin": user_key['wxuin'], "Sid": user_key['wxsid'], "Skey": user_key['skey'], "DeviceID": "e823192381686302"}, "Msg": {"Type": 47, "FromUserName": user['UserName'], "EmojiFlag":2, "MediaId":mediaid, 'ClientMsgId':ctime, 'LocalID':ctime, "ToUserName": tousername,}, "Scene": 0} send_res = requests.post(sendurl, json=send_data,cookies=cookice).json() return send_res