返回JSON以插入SQLite数据库时使用偏移量和限制

时间:2016-09-26 12:11:47

标签: python json

我正在尝试如何使用python从api获取json并将其插入SLQlite数据库。我可以为有限数量的记录做到这一点,但我遇到的问题是我使用的api需要限制和偏移才能获得大量记录。我的python需要一些帮助 - 感谢任何建议。

json是这样的:

{ "meta": {
        "result_count":1025 ,
        "cluster_counts" : {},
        "clusters" : [] ,
        "group_details": [] },
  "records": [
    {
        "pk": 19577,
        "model": "collection.museumobject",
        "fields": {
            "primary_image_id": "2006AJ6728",
            "rights": 3,
            "year_start": 1600,
            "object_number": "O61539",
            "artist": "Fiamengo, Iacopo",
            "museum_number": "W.36:1, 2-1981",
            "object": "Cabinet",
            "longitude": "14.25185000",
            "last_processed": "2016-07-30 05:37:55",
            "event_text": "",
            "place": "Naples",
            "location": "Europe 1600-1815, room 6, case CA11",
            "last_checked": "2016-07-30 05:37:55",
            "museum_number_token": "w361981",
            "latitude": "40.83990100",
            "title": "",
            "date_text": "about 1600 (Made)\nca. 1600 (made)",
            "slug": "cabinet-fiamengo-iacopo",
            "sys_updated": "2016-07-07 00:00:00",
            "collection_code": "FWK"
        }
    }

我的代码尝试是这样的:

import json
import urllib

offset = 0
while True:
    url = "http://www.vam.ac.uk/api/json/museumobject/search?limit=45&offset=%d" % offset
    print 'Retrieving' , url

    connection = urllib.urlopen (url)
    data = connection.read()

    js = json.loads(data)
    #print json.dumps(js, indent=4)

    if js ['records']:
        print 'incrementing offset'
        offset +=50
        for item in js['records']:
            artist = item['fields']['artist'];
            object = item ['fields']['object']

            print artist, object

0 个答案:

没有答案