簡體   English   中英

Python發布請求導致403錯誤

[英]Python post requests causing 403 Error

我目前在發出instagram的發布請求時使用請求python libraray遇到403錯誤。

我正在嘗試使用“ https://www.instagram.com/web/friendships/2663753/follow/ ”跟蹤用戶,該URL是我從檢查元素的網絡頁面獲得的。 這需要POST請求。

Remote Address:31.13.77.52:443
Request URL:https://www.instagram.com/web/friendships/313746262/unfollow/
Request Method:POST
Status Code:200 OK
Response Headers
view source
Cache-Control:private, no-cache, no-store, must-revalidate
Connection:keep-alive
Content-Language:en
Content-Length:15
Content-Type:application/json
Date:Thu, 24 Dec 2015 03:58:08 GMT
Expires:Sat, 01 Jan 2000 00:00:00 GMT
Pragma:no-cache

Strict-Transport-Security:max-age=86400
Vary:Cookie, Accept-Language
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0

Host:www.instagram.com
Origin:https://www.instagram.com
Referer:https://www.instagram.com/darealbre/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36
X-CSRFToken:9776e6b7b249da6bd12397ef21a11928
X-Instagram-AJAX:1
X-Requested-With:XMLHttpRequest

我已經在傳遞Cookie,這是我的代碼。

cookies_list = {'target_sig': '', 'ig_pr': '1', 'target': '', 'mid': 'VnttxgAEAAGrb1koHc8WHSK0qeyk', 'ds_user_id': '2352930331', 'sessionid': 'IGSC616b97e4de9eaabee722cb3b0c3758232253dcbea19983ea8ae7b52b1d2b9486%3AgBJyWG66Wqx3ZhebzN6mJ6ydhzPzlrcr%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id%22%3A2352930331%2C%22_token%22%3A%222352930331%3AEhfw99oCaIp8a6TiOjbbSHhzg6QwfV54%3Ab645bc518c898fc02c06911e79c531475c5c9f5b3fbbac054281ea6bfe0adb7d%22%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1450929608.312806%2C%22_platform%22%3A4%7D', 'ig_vw': '1680', 'csrftoken': '28de9e973017aae25868be36e0f2fffa', 's_network': ''}

    headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36','X-CSRFToken':'9776e6b7b249da6bd12397ef21a11928','Cookie'}
    post_requests = requests.post('https://www.instagram.com/web/friendships/2663753/follow/',cookies=cookies_list,headers=headers)

我登錄instagram后就得到了cookie。 但是,它不斷返回我403錯誤!

使用CURL,我將CURL命令粘貼到終端中,它跟隨用戶!

"curl 'https://www.instagram.com/web/friendships/330108492/follow/' -X POST -H 'Cookie: mid=VnnkDAAEAAGX-EXpxDkSsYIpk3n6; fbm_124024574287414=base_domain=.instagram.com; sessionid=IGSC88d8406c0f8996d4356abddcd986235bac1dc969a6604d8c9a205e39b95286d0%3AOGn68bqGJmIXQqLvq0i9eFbPmATqZkl5%3A%7B%22_token_ver%22%3A2%2C%22_auth_user_id%22%3A2352930331%2C%22_token%22%3A%222352930331%3A6DxouHjZAEq73UIarTseawN0uq8oKpLq%3Aba8a7b0655113764a9c768a62a9da856d7894686af3c6d50356d8819ad7caa52%22%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1450918827.64184%2C%22_platform%22%3A4%7D; ig_pr=1; ig_vw=1855; s_network=; csrftoken=9776e6b7b249da6bd12397ef21a11928; ds_user_id=2352930331; fbsr_124024574287414=Pv5CMlvyUnfLDZIM9oQibeNkRRGNlwu7JUEO9mtdyFs.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiJBUUI4d281MW8xc09HVnV3RGlLcWpsYkhVQWlqUUdmWXpBc2YwTFo2WTZQR19iV2dQeGs4N3gwaHNId1NzTkh2Mi1WSUFleTlIajRrOEp2QXl4SnhuZzZYUjF1MGJBXzh4c1IyRndWWW0xLTRoMkczdlpQYnExSnk5bVpsT3NZSnpCUmF2UjZpbmx0bjBaZU0zZTlFTnRxaTd0V1BwVUhGbmw4aGM4R1ZSbU5DRElFQmN2MkQ4LVVQa3BIVmtGRDRJeGNsQ0JWOVQtcG1xNGVIcWlYcWZIeE1jVTNKRU5kaUkyUUR0bUdabzVXSGxMY0xvV2Y0TlNoU3RwQmMwS2dmM0VFYTVmQ3JSWGFNMGtwdzhiZEpKMjN3dG8xSmd5b2RYS0RkU080UnBkSC1VNXJBTkgtbXI5Umhja2gzTzNJU2ZvRGxBVnBfVnZhWld6b2dQZXVhNGh6NSIsImlzc3VlZF9hdCI6MTQ1MDkzOTU1NiwidXNlcl9pZCI6IjEwMDAwMDgyMDI2MDE2MiJ9' -H 'Origin: https://www.instagram.com' -H 'Accept-Encoding: gzip, deflate' -H 'X-Instagram-AJAX: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.8' -H 'Accept: */*' -H 'Referer: https://www.instagram.com/struckbyfashion/' -H 'X-CSRFToken: 9776e6b7b249da6bd12397ef21a11928' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' -H 'Content-Length: 0' --compressed" 

那是我的卷發返回的信息

所以首先您不應該發布您的cookie,因為它使任何人都可以訪問您的instagram帳戶。 確保盡快注銷以使該cookie無效,然后再次登錄並保持該cookie私有。

其次,不要同時使用cookies_list 只需將所有內容作為標頭傳遞如下:

headers = {
    'Cookie': '<Paste Everything in the Curl "Cookie" header here>',
    'Origin': 'https://www.instagram.com',
    'Accept-Encoding': 'gzip, deflate',
    'X-Instagram-AJAX': 1,
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36,(KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
    'Accept-Language': 'en-US,en;q=0.8',
    'Accept': '*/*',
    'Referer': 'https://www.instagram.com/struckbyfashion/',
    'X-CSRFToken': '9776e6b7b249da6bd12397ef21a11928',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive',
    'Content-Length': 0
}

post_requests = requests.post('https://www.instagram.com/web/friendships/2663753/follow/', headers=headers)
print(post_requests.text)  # {"status":"ok"}
print(post_requests.status_code)  # 200

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM