python从json数据提取列表到tex

2024-10-01 00:28:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个json数据https://pastebin.com/W422EtLn,返回表单关键字工具.ioapi,需要提取文本文件中的所有字符串,我的实际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')

需要输出>

^{pr2}$

Tags: 数据httpsioapijsonhttpurldata
1条回答
网友
1楼 · 发布于 2024-10-01 00:28:06

如果您只想从json中的“results”中“提取所有字符串元素”:

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()

输出:

^{pr2}$

相关问题 更多 >