简体   繁体   English

如何在调用API时避免或跳过python中的错误400

[英]How to avoid or skip error 400 in python while calling the API

Note:- I have written my code after referring to few examples in stack overflow but still could not get the required output 注意:-我在引用堆栈溢出中的几个示例后编写了代码,但仍然无法获得所需的输出

I have a python script in which loop iterates with an Instagram API. 我有一个Python脚本,其中循环使用Instagram API进行迭代。 I give the user_id as an input to the API which gets the no of posts, no of followers and no of following. 我提供了user_id作为API的输入,该API获得了帖子的数量,关注者的数量和关注者的数量。 Each time it gets a response, I load it into a JSON schema and append to lists data1, data2 and data3. 每次收到响应时,我都将其加载到JSON模式中,并追加到列表data1,data2和data3。

The issue is:= Some accounts are private accounts and the API call is not allowed to it. 问题是:=有些帐户是私人帐户,不允许对其进行API调用。 When I run the script in IDLE Python shell, its gives the error 当我在IDLE Python Shell中运行脚本时,它给出了错误

Traceback (most recent call last):
  File "<pyshell#144>", line 18, in <module>
beta=json.load(url)
File "C:\Users\rnair\AppData\Local\Programs\Python\Python35\lib\site-  packages\simplejson-3.8.2-py3.5-win-amd64.egg\simplejson\__init__.py", line 455,  in load
 return loads(fp.read(),
File "C:\Users\rnair\AppData\Local\Programs\Python\Python35\lib\tempfile.py", line 483, in func_wrapper
return func(*args, **kwargs)
**ValueError: read of closed file**

But the JSON contains this:- 但是JSON包含以下内容:

{
  "meta":  {
  "error_type": "APINotAllowedError",
  "code": 400,
  "error_message": "you cannot view this resource"
 }
}

My code is:- 我的代码是:-

for r in range(307,601):
 var=r,sheet.cell(row=r,column=2).value
 xy=var[1]
 ij=str(xy)
 if xy=="Account Deleted":
    data1.append('null')
    data2.append('null')
    data3.append('null')
    continue
myopener=Myopen()
try:           
    url=myopener.open('https://api.instagram.com/v1/users/'+ij+'/?access_token=641567093.1fb234f.a0ffbe574e844e1c818145097050cf33')
except urllib.error.HTTPError as e:  // I want the change here
    data1.append('Private Account')
    data2.append('Private Account')
    data3.append('Private Account')
    continue
beta=json.load(url)
item=beta['data']['counts']
data1.append(item['media'])
data2.append(item['followed_by'])
data3.append(item['follows'])

I am using Python version 3.5.2. 我正在使用Python版本3.5.2。 The main question is If the loop runs and a particular call is blocked and getting this error, how to avoid it and keep running the next iterations? 主要问题是, 如果循环运行并且特定的调用被阻止并收到此错误,如何避免它并继续运行下一个迭代? Also, if the account is private, I want to append "Private account" to the lists. 另外,如果该帐户是私人帐户,我想将“私人帐户”附加到列表中。

Looks like the code that is actually fetching the URL is within your custom type - "Myopen" (which is not shown). 看起来实际上是在获取URL的代码在您的自定义类型-“ Myopen”(未显示)中。 It also looks like its not throwing the HTTPError you are expecting since your "json.load" line is still being executed (and leading to the ValueError that is being thrown). 它也像仍在执行其不扔你期待的HTTPError,因为你的“json.load”行(并导致被抛出ValueError异常)。

If you want your error handling block to fire, you would need to check the response status code to see if its != 200 within Myopen and throw the HTTPError you are expecting instead of whatever its doing now. 如果要触发错误处理块,则需要检查响应状态代码以查看Myopen中的!= 200,并抛出您期望的HTTPError而不是现在执行的任何操作。


I'm not personally familiar with FancyURLOpener , but it looks like it supports a getcode method. 我个人不熟悉FancyURLOpener ,但看起来它支持getcode方法。 Maybe try something like this instead of expecting an HTTPError: 也许尝试这样的事情,而不是期望HTTPError:

url = myopener.open('yoururl')
if url.getcode() == 400:
  data1.append('Private Account')
  data2.append('Private Account')
  data3.append('Private Account')
  continue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 fetch 方法调用 Python Flask 中开发的 Post API 时,REACT JS 出现 400 错误 - 400 Error in REACT JS while calling Post API developed in Python Flask using fetch method 在 python 中获取 api 时如何避免这些错误 - How to avoid these errors while fetching api in python 如何避免在循环中调用 api? - How to avoid calling a api in loops? 从 python 命令行调用 google api 时出错 - error while calling google api from python command line 尝试使用python中的请求访问REST API时出现错误请求(400)错误 - Bad Request (400) error while trying to access REST API using requests in python GitHub API上的Python“请求”模块400错误 - Python “Requests” Module 400 error on GitHub API 如何跳过python多处理中的错误 - how to skip error in python multiprocessing 如何在调用谷歌分析 API 时删除 python 中的额外引号 - How to remove extra quotes in python while calling google analytics API Python - 如何在仍然获取数据的同时避免Pandas中的错误(异常)? - Python - How to avoid error (Exceptions) in Pandas while still getting data ? AWS 自定义联合代理:调用联合端点错误 400 python - AWS Custom Federation Broker: calling federation endpoint error 400 python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM