Python 使用标准JSON和urllib2从网站获取JSON对象

Python 使用标准JSON和urllib2从网站获取JSON对象,python,json,urllib2,Python,Json,Urllib2,我编写了一段代码,使用JSON和请求从github网站提取JSON对象: #!/usr/bin/python import json import requests r = requests.get('https://github.com/timeline.json') #Replace with your website URL with open("a.txt", "w") as f: for item in r.json or []: try:

我编写了一段代码,使用JSON和请求从github网站提取JSON对象:

#!/usr/bin/python

import json
import requests

r = requests.get('https://github.com/timeline.json') #Replace with your website URL

with open("a.txt", "w") as f:
    for item in r.json or []:
        try:
            f.write(item['repository']['name'] + "\n") 
        except KeyError: 
            pass  

这个很好用。但是,我想使用urllib2和标准json模块做同样的事情。我该怎么做?谢谢。

只需下载数据并使用Python的:


谢谢你的回答。但是我在运行代码时遇到了这个错误:对于r.json或[]:AttributeError中的项:'str'对象没有属性'json',那么,您必须自己解析json。更新。
import json
import urllib2
r = urllib2.urlopen('https://github.com/timeline.json')

with open("a.txt", "w") as f:
    for item in json.load(r) or []:
        try:
            f.write(item['repository']['name'] + "\n") 
        except KeyError:
            pass