requests获得json数据,此处为requests演示
#-*- coding:utf-8 -*-
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print r.text
返回的数据格式为:
{
"errno": "0",
"count": "69",
"data": [
{
"id": "123456",
"create_time": "2016-03-28 11:41:00",
"phone": "138888000",
"name": "路人甲",
"level": "358",
"login_ip": null
},
{
"id": "456789",
"create_time": "2016-03-30 11:41:00",
"phone": "138888111",
"name": "炮兵灰",
"level": "123",
"login_ip": null
}
]
}
MYSQL设置部分
import MySQLdb as mdb
con = mdb.connect('localhost', 'root', 'root', 'testdb');
#只需要data中的id,create_time,phone,name,这4个
cur.execute("CREATE TABLE ...")
#这里怎么创建表
cur.execute("INSERT INTO......")
#这里怎么读取requests获得json数据,并且写进来
#只需要data中的id,create_time,phone,name,这4个
高洛峰2017-04-18 09:25:11
Your data is a standard json string. You can use the json library to convert it into a json object (dict in python). Here is an example:
>>> import json
>>> ss = json.loads('{"errno":"0", "data":[{"id":"23423","name":"qqqq"},{"id":"24325", "name":"dfghfh"}]}')
>>> ss
{u'errno': u'0', u'data': [{u'id': u'23423', u'name': u'qqqq'}, {u'id': u'24325', u'name': u'dfghfh'}]}
>>> ss['errno']
u'0'
>>> ss['data'][0]['id']
u'23423'
阿神2017-04-18 09:25:11
r.json()
Also remember to use requests.Session
, it will be twice as fast for the same server.