python 2.7
Почему так работает:
import urllib, json
url_members = 'https://api.vk.com/method/groups.getMembers?group_id=67824212'
for j in range(3):
response_url_members = urllib.urlopen(url_members)
members = json.loads(response_url_members.read())['response']['users'][j]
print(members)
А если вынести переменную response_url_members из цикла ошибка:
import urllib, json
url_members = 'https://api.vk.com/method/groups.getMembers?group_id=67824212'
response_url_members = urllib.urlopen(url_members)
for j in range(3):
members = json.loads(response_url_members.read())['response']['users'][j]
print(members)
Traceback (most recent call last):
File "/home/eq/PycharmProjects/vk_stat_users/main.py", line 30, in <module>
members = json.loads(response_url_members.read())['response']['users'][j]
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Первый вариант не подходит - медленный. Что можно сделать?
upd: print в обоих вариантах добавил в цикл