Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python post请求导致403错误_Python_Http_Cookies_Python Requests - Fatal编程技术网

Python post请求导致403错误

Python post请求导致403错误,python,http,cookies,python-requests,Python,Http,Cookies,Python Requests,我目前在为instagram发出post请求时,使用请求python libraray时遇到403错误 我正在尝试使用从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 Respo

我目前在为instagram发出post请求时,使用请求python libraray时遇到403错误

我正在尝试使用从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就得到了饼干。然而,它不断返回我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\u列表
。只需将所有内容作为标题传递到此处,如下所示:

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
标题={
“饼干”:“,
“来源”:https://www.instagram.com',
“接受编码”:“gzip,deflate”,
“X-Instagram-AJAX”:1,
“用户代理”:“Mozilla/5.0(X11;Linux x86_64)AppleWebKit/537.36,(KHTML,如Gecko)Chrome/41.0.2272.101 Safari/537.36”,
‘接受语言’:‘en-US,en;q=0.8’,
“接受”:“*/*”,
“Referer”:https://www.instagram.com/struckbyfashion/',
“X-CSRFToken”:“9776E6B7B249 DA6BD12397EF21A11928”,
“X-request-With':“XMLHttpRequest”,
“连接”:“保持活动状态”,
“内容长度”:0
}
post_请求=请求。post('https://www.instagram.com/web/friendships/2663753/follow/,headers=headers)
打印(post_requests.text)#{“状态”:“ok”}
打印(post_请求。状态代码)#200

所以首先你不应该发布你的cookie,因为它让这里的任何人都可以访问你的instagram帐户。确保尽快注销以使此cookie无效,然后再次登录并保持该cookie的私有性

其次,不要同时使用
cookies\u列表
。只需将所有内容作为标题传递到此处,如下所示:

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
标题={
“饼干”:“,
“来源”:https://www.instagram.com',
“接受编码”:“gzip,deflate”,
“X-Instagram-AJAX”:1,
“用户代理”:“Mozilla/5.0(X11;Linux x86_64)AppleWebKit/537.36,(KHTML,如Gecko)Chrome/41.0.2272.101 Safari/537.36”,
‘接受语言’:‘en-US,en;q=0.8’,
“接受”:“*/*”,
“Referer”:https://www.instagram.com/struckbyfashion/',
“X-CSRFToken”:“9776E6B7B249 DA6BD12397EF21A11928”,
“X-request-With':“XMLHttpRequest”,
“连接”:“保持活动状态”,
“内容长度”:0
}
post_请求=请求。post('https://www.instagram.com/web/friendships/2663753/follow/,headers=headers)
打印(post_requests.text)#{“状态”:“ok”}
打印(post_请求。状态代码)#200

您的URL显示unfollow,而不是follow,但403表示禁止。因此,也许你需要一个API密钥来实现这一点。我的建议是使用“Chrome开发工具”,并在“follow”请求发出后右键单击。单击“复制为Curl请求”,将其粘贴到某个位置,并对其进行分析,以查看您做错了什么。@Dan不,这与API无关。我在谷歌上问过这个问题,大部分答案都是由于禁止访问。我正在传递cookies,正确地传递标题。不幸的是,所有这些似乎对我都不起作用。@martin,这没有帮助,因为我已经在这样做了!如果curl请求在您从终端运行时有效,但python请求无效,则表明您做错了。您的URL显示unfollow,而不是follow,但403表示禁止。因此,也许你需要一个API密钥来实现这一点。我的建议是使用“Chrome开发工具”,并在“follow”请求发出后右键单击。单击“复制为Curl请求”,将其粘贴到某个位置,并对其进行分析,以查看您做错了什么。@Dan不,这与API无关。我在谷歌上问过这个问题,大部分答案都是由于禁止访问。我正在传递cookies,正确地传递标题。不幸的是,所有这些似乎对我都不起作用。@martin,这没有帮助,因为我已经在这样做了!如果curl请求在您从终端运行时有效,但您的python请求无效,则表明您做错了什么。嘿,伙计,问过您吗,您说过我实际上遇到了相同的错误。上面还写着403!它会起作用的。。如果您已注销,请确保您正在使用新会话中的cookie。请确保也使用新的csrf令牌{“状态”:“失败”,“消息”:“未经授权”,“重定向url”:“\/accounts\/login\/?next=\/web\/friendships\/3931765\/follow\/”}403我目前正忙于从一个url获取X-CSRFToken和cookie,并使用请求将其传递到另一个url!好的,请详细说明我的问题。我有两个URL-一旦我登录,我会存储我的cookie以备将来的请求。但是,我不知道如何使用所有这些信息访问请求头数据,例如cookie、X-CSRFToken、X-request-header。因此,我有多个帐户,我想遵循,他们都有不同的Cookie/csrftoken。所以我想知道我如何才能把这些放在我的请求中,让他们通过!嘿,老兄,你问过我吗?你说我真的遇到了同样的错误。上面还写着403!它会起作用的。。如果您已注销,请确保您正在使用新会话中的cookie。请确保也使用新的csrf令牌{“状态”:“失败”,“消息”:“未经授权”,“重定向url”:“\/accounts\/login\/?next=\/web\/friendships\/3931765\/follow\/”}403我目前正忙于从一个url获取X-CSRFToken和cookie,并使用请求将其传递到另一个url!好的,请详细说明我的问题。我有两个URL-一旦我登录,我会存储我的cookie以备将来的请求。但是,我不知道如何使用所有这些信息访问请求头数据,例如cookie、X-CSRFToken、X-request-header。因此,我有多个帐户,我想遵循,他们都有不同的Cookie/csrftoken。所以我想知道我如何才能把这些放在我的请求中,让他们通过!