0
行,有是返回这样一个JSON格式的API:Python不将结果保存到数据库
{
"count": 111,
"people": [
{
"id": 2,
"name": "kael",
"owner": {
"login": "[email protected]",
"id": 112
},
"followers": 18856,
"popularity": 1.0
},{
"id": 2,
"name": "das",
"owner": {
"login": "[email protected]",
"id": 122
},
"followers": 188356,
"popularity": 133
}
]
}
我试图从URL http://api.example.com/people/get
获取这些并把结果提交给数据库。为此,这是代码。
import json, MySQLdb, urllib2, time
# database object
db = MySQLdb.connect("localhost","root","foo","bar")
x = db.cursor()
# API url
url = "http://api.example.com/people/get"
response = urllib2.urlopen(url)
jsonString = response.read()
toJson = json.loads(jsonString)
# result counts
total_count = toJson['count']
i = 1
pages = total_count/100
while (i < pages):
_url = "http://api.example.com/people/get/page"+ str(i) +"&per_page=100"
_response = urllib2.urlopen(_url)
_jsonString = _response.read()
_toJson = json.loads(_jsonString)
user = _toJson['people']
for r in user:
user_id = r['id']
name = r['name']
full_name = r['full_name']
url = r['url']
try:
x.execute("""INSERT INTO repos (id, name, f_name, url)
VALUES ('%d', '%s', '%s', '%s') """, (user_id, name, full_name , url))
db.commit()
except:
db.rollback()
i += 1
db.close()
我不知道上面的脚本是否有逻辑问题,因为它不工作。没有任何数据被提交到数据库。
您应该添加调试输出到你的脚本,特别是到了'try' /'except'。使用'logging'模块或者简单地'print'! –
pages = total_count/100是111/100。不太了解python,但本轮不到1,所以你的if不会被触发 – Mihai
@Mihai结果实际上超过了5K,我对这个例子做了两个查询 –