从json数据到文本的python提取列表

时间:2016-01-13 12:56:29

标签: python json

我有这个json数据https://pastebin.com/W422EtLn,返回表单keywordtool.io api,需要解压缩文本文件中的所有字符串,我的acctual python代码是这样的:

#http://keywordtool.io/api/documentation

def get_data(keyword):
    url = 'http://api.keywordtool.io/v1/search/google?apikey=[api]&keyword={keyword}&country={country}&language={language}&output=json'\
        .format(keyword=keyword, country='it', language='it')
    data = json.loads(urllib2.urlopen(url).read())
    pprint.pprint(data)

get_data('roma')

需要输出>

0 romano
roma 0 12
roma 0-2 man utd
roma 0 lazio 1

1 个答案:

答案 0 :(得分:0)

如果您只想提取所有字符串元素'来自'结果'在json:

outFile = open('output.txt', 'w')
for element in data['results']:
    tempList = data['results'][element]
    for dict in tempList:
        #print(dict['string'])
        out.write(dict['string']+'\n')

out.close()

输出:

h&m romania
h romadske tv
roma h&m
roma 2 bologna 3
(etc.)