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

问题描述 投票:1回答:1

我正在尝试使用python post请求更改我的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)
python post request python-requests instagram
1个回答
1
投票
  1. 通过firefox开发人员工具嗅探上传过程后,我将所有request headers复制到headersof post request,但它没有用,然后我删除了Content-Type": "multipart/form-data; boundary=-...XXXXX"并繁荣!
  2. 我还用"Content-Length"更新了os.path.getsize("my_new_pp.jpg")
  3. 确保刷新没有缓存的配置文件图片页面,在FF上,按住Shift键并左键单击“重新加载”按钮。

我设法使用以下代码在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"}
© www.soinside.com 2019 - 2024. All rights reserved.