简体   繁体   English

python处理来自Wikipedia API的大型json响应

[英]python handling large json response from wikipedia api

I am accessing the wikipedia api to grab the text from a page. 我正在访问Wikipedia API,以从页面中获取文本。 I'm using the parse api call with the page name. 我正在使用带有页面名称的parse api调用。 Click here for example, then hit the make request button to get the response. 例如,单击此处,然后单击“生成请求”按钮以获取响应。 It gives you the html of the whole site as an element in the json object and allows you to parse the items you need by providing a byte offset for each section in the wiki page. 它为您提供整个站点的html作为json对象中的元素,并允许您通过为Wiki页面中的每个部分提供字节偏移量来解析所需的项目。 Is there a better way to handle this then to load the whole response into memory? 有没有更好的方法来处理此问题,然后将整个响应加载到内存中? Right now, all I can think of is to use json.loads() to create a dict and then split the string by each of the byte offset it specifies. 现在,我能想到的就是使用json.loads()创建一个dict,然后将字符串除以它指定的每个字节偏移量。

You could use iijson , an iterative JSON parser. 您可以使用iijson (迭代JSON解析器)。 This package allows you to iterate over nodes, for example: 该软件包允许您遍历节点,例如:

import ijson

f = urlopen('http://.../')
objects = ijson.items(f, 'earth.europe.item')
cities = (o for o in objects if o['type'] == 'city')
for city in cities:
   do_something_with(city)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM