Python使用请求更改Instagram个人资料图片

时间:2018-11-27 13:57:50

标签: python post request python-requests instagram

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

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 个答案:

答案 0 :(得分:0)

  1. 通过firefox开发人员工具嗅探上传过程后,我复制了 request headers的{​​{1}}至headers中的所有post request Content-Type": "multipart/form-data; boundary=-...XXXXX"和繁荣!
  2. 我还用以下方式更新了"Content-Length"值: os.path.getsize("my_new_pp.jpg")
  3. 确保在FF上使用无缓存刷新个人资料图片页面,按住 Shift键并按住左键, kbd>重新加载 按钮。

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

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"}