2016-01-13 96 views
0

我有這樣的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 

回答

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.) 
+0

非常感謝你 – kingcope