如何克服"ValueError:读取关闭的文件"在python 3.5.1中 [英] how to overcome "ValueError: read of closed file" in python 3.5.1

查看:318
本文介绍了如何克服"ValueError:读取关闭的文件"在python 3.5.1中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本来获取编号. instagram API中的帖子,关注者和关注者.我第一次运行该脚本时,它可以完美运行并提供了所需的数据. 脚本是:-

I have a python script which fetches the no. of posts, followers and follows from the instagram API. The first time I ran the script, it worked perfect and gave me the required data. The script is:--

for r in range(10,12):
    var=r,sheet.cell(row=r,column=2).value
    xy=var[1]
    ij=str(xy)
    myopener=Myopen()
    url=myopener.open('https://api.instagram.com/v1/users/'+ij+'/?access_token=641567093.1fb234f.a0ffbe574e844e1c818145097050cf33')
    beta=json.loads(url)
    item=beta['data']['counts']
    data1.append(item['media'])
    data2.append(item['followed_by'])
    data3.append(item['follows'])

我已声明我的FancyURLopener如下:=

I have declared my FancyURLopener as follows:=

class Myopen(FancyURLopener):
version='Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'

第二次运行脚本时,它开始向我显示此错误:-

The second time I ran the script it started showing me this error:-

Traceback (most recent call last):
File "<pyshell#39>", line 7, 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

如何克服这个问题?我已经使用相同的脚本一个星期了,它从未给出任何错误.为什么是今天?

How to overcome this? I have been using the same script for a week now and it never gave any error. Why today?

推荐答案

我自己发现了!

上面收到的错误是由于: 很少有instagram私人帐户.因此,不允许对这些帐户进行API调用,由于JSON试图读取该API调用,它将显示一个值错误,并且将包含HTTP错误代码400.

The error which was received above was due to:- There were few instagram accounts which were private. So the API call is not allowed to those accounts and it will show a value error because of the JSON trying to read it and will contain HTTP error code 400.

为克服这个问题,我像这样更改了代码:-

To overcome this I changed my code like this:-

for r in range(1501,1625):
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()
url=myopener.open('https://api.instagram.com/v1/users/'+ij+'/?access_token=641567093.1fb234f.a0ffbe574e844e1c818145097050cf33')
if url.getcode() == 400:
    data1.append('Private Account')
    data2.append('Private Account')
    data3.append('Private Account')
    continue
else:
    beta=json.load(url)
    item=beta['data']['counts']
    data1.append(item['media'])
    data2.append(item['followed_by'])
    data3.append(item['follows'])

从以上内容获得帮助的任何人都可以问我有关此问题的任何疑问!

Anybody taking help from the above can feel free to ask me any queries regarding the same!

这篇关于如何克服"ValueError:读取关闭的文件"在python 3.5.1中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆