Python发布请求导致403错误

时间:2015-12-24 04:02:16

标签: python http cookies python-requests

我目前面临403错误使用请求python libraray同时发布Instagram请求。

我试图关注用户使用" https://www.instagram.com/web/friendships/2663753/follow/"我从inspect元素中的网页获取的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" 

我的卷发回复了信息

1 个答案:

答案 0 :(得分:1)

首先,您不应该发布您的Cookie,因为它可以让任何人访问您的Instagram帐户。确保您注销ASAP以使此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