Python使用请求更改Instagram个人资料图片 [英] Python change instagram profile-picture with requests

查看:99
本文介绍了Python使用请求更改Instagram个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python发布请求更改我的Instagram个人资料图片,但它没有更改.我没有任何错误,并且在回复中说,他们对此进行了更改,但更改为一张白色的Instagram个人头像,而不是我选择的头像.

I'm trying to change my Instagram profile picture with a python post request, but it does not change. I don't get any errors and in the response, it says that they changed it, but to a white Instagram person picture and not my chosen picture.

request url: https://www.instagram.com/accounts/web_change_profile_picture/

request headers: :authority: www.instagram.com
:method: POST
:path: /accounts/web_change_profile_picture/
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7
content-length: 251409
content-type: multipart/form-data; boundary=----WebKitFormBoundarySbMgVbqkrGZYUfnE
cookie: ig_cb=1; mid={mid}; mcd=3; rur=ATN; ig_gdpr_signup=1; csrftoken={my_csrf}; shbid=9320; shbts=1543326033.1119306; ds_user_id={my_userid}; sessionid=3215216108%3A3qIcf1SYxlvIOd%3A29; urlgen="{my_urlgen}:7zHjeu:bVP01Os-2jGH6RETg-jCpkDsaRf"
origin: https://www.instagram.com
referer: https://www.instagram.com/{username}/
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36
x-csrftoken: {my_csrf}
x-instagram-ajax: {my_ajax}
x-requested-with: XMLHttpRequest

Form data: 
profile_pic: (binary)

View Source form data:
------WebKitFormBoundarySbMgVbqkrGZYUfnE
Content-Disposition: form-data; name="profile_pic"; filename="profilepic.jpg"
Content-Type: image/png


------WebKitFormBoundarySbMgVbqkrGZYUfnE--

My code:
s.headers.update(headers_above)
img_payload = {
              "file": ("blo.jpg", open("D:\\{path_to_jpg_file}", "rb"), "image/jpeg")
                    }
img_url = "https://www.instagram.com/accounts/web_change_profile_picture/"
 r3 = s.post(img_url, files=img_payload)

推荐答案

  1. 通过firefox开发人员工具嗅探上传过程后,我复制了 所有request headersheadersheaders,但是没有用,于是我删除了 Content-Type": "multipart/form-data; boundary=-...XXXXX"和繁荣!
  2. 我还用"Content-Length"值更新了 os.path.getsize("my_new_pp.jpg")
  3. 确保在FF上使用无缓存刷新个人资料图片页面,按住 Shift键并按住左键 重新加载 按钮.
  1. After sniffing the upload process via firefox developer tools, I copied all the request headers to the headersof post request, but it didn't work, then I removed Content-Type": "multipart/form-data; boundary=-...XXXXX" and boom!
  2. I also updated the "Content-Length" value with os.path.getsize("my_new_pp.jpg")
  3. Make sure you refresh the profile pic page with no cache, on FF, hold down the Shift key and left-click the Reload button.

我设法使用以下代码在instagram上更改了我的个人资料照片:

I managed to change my profile pic on instagram using the following code:

import os
import requests

p_pic = "my_new_pp.jpg"
p_pic_s = os.path.getsize("my_new_pp.jpg")

headers = {
"Host": "www.instagram.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://www.instagram.com/pedro_lobito/",
"X-CSRFToken": "YOUR_X-CSRFToken",
"X-Instagram-AJAX": "YOUR_X-Instagram-AJAX",
"X-Requested-With": "XMLHttpRequest",
"Content-Length": str(p_pic_s),
"DNT": "1",
"Connection": "keep-alive",
"Cookie": "YOUR_COOKIE"
}

url = "https://www.instagram.com/accounts/web_change_profile_picture/"

files = {'profile_pic': open(p_pic,'rb')}
values = {"Content-Disposition": "form-data", "name": "profile_pic", "filename":"profilepic.jpg",
"Content-Type": "image/jpeg"}

r = requests.post(url, files=files, data=values, headers=headers)
print(r.text)


回复:

{"changed_profile": true, "id": 233885295, "has_profile_pic": true, "profile_pic_url": "https://instagram.flis9-1.fna.fbcdn.net/vp/09bb6c124303a825a67da0cb092c9ee7/5C8F561F/t51.2885-19/s150x150/44814766_1606677162811158_2721039563597283328_n.jpg", "profile_pic_url_hd": "https://instagram.flis9-1.fna.fbcdn.net/vp/adb01e186d733d3660c300db5bae41a9/5C79DA67/t51.2885-19/s320x320/44814766_1606677162811158_2721039563597283328_n.jpg", "status": "ok"}

这篇关于Python使用请求更改Instagram个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆