Python-创建循环以更改api搜索结果并写入JSON / CSV

时间:2018-09-02 16:33:05

标签: python json csv

我完全迷失了如何解决我的问题。到目前为止,我可以在“ target_id”中使用一个请求写入JSON文件。该API将自动输出JSON。

问题是,如果我有一个target_id的列表(当前以JSON和CSV格式),如何循环它以更改target_id的列表(也就是更改API URL),然后写入同一文件?

代码:

import urllib.parse
import requests
import json

main_api = 'http://sc-api.com/?api_source=live&system=accounts&action=full_profile&'

target_id = 'abc'

url = main_api + urllib.parse.urlencode({'target_id': target_id})
json_data = requests.get(url).json()
json_status = json_data['request_stats']['query_status']

#-----did the API work?-----
#print('API Status: ' + json_status)
#print()
#print(json_data)
#print()

handle = json_data['data']['handle']
moniker = json_data['data']['moniker']
citizen_number = json_data['data']['citizen_number']

#-----preview if no need to write to file-----
#print('target_id: ' + handle + ',', 'Moniker: ' + moniker + ',', 'Citizen Number: ' + citizen_number)

with open('new_test.json', 'w') as f:
    json.dump((handle, moniker, citizen_number), f)

以下是包含目标ID列表的文件格式:

 [
      {
        "Handle": "name1"
      },
      {
        "Handle": "name2"
      },
      {
        "Handle": "name3"
      },
      {
        "Handle": "name4"
      },
      {
        "Handle": "name5"
      }
    ]

任何对此的帮助或与之相关的帮助将不胜感激。谢谢:)

1 个答案:

答案 0 :(得分:1)

您可以像使用以下这样在文件上循环:

import urllib.parse
import requests
import json

main_api = 'http://sc-api.com/?api_source=live&system=accounts&action=full_profile&'

with open('data.json') as f:
    data = json.load(f)

    result = []
    for element in data:
        target_id = element['Handle']

        url = main_api + urllib.parse.urlencode({'target_id': target_id})
        json_data = requests.get(url).json()
        json_status = json_data['request_stats']['query_status']

        handle = json_data['data']['handle']
        moniker = json_data['data']['moniker']
        citizen_number = json_data['data']['citizen_number']

        # -----preview if no need to write to file-----
        print('target_id: ' + handle + ',', 'Moniker: ' + moniker + ',', 'Citizen Number: ' + citizen_number)

        result.append({'target_id': handle, 'Moniker': moniker, 'Citizen Number': citizen_number})

    with open('output.json', 'w') as f:
        json.dump(result, f)

输出

target_id: abc, Moniker: C4sher, Citizen Number: 210602
target_id: abc, Moniker: C4sher, Citizen Number: 210602
target_id: abc, Moniker: C4sher, Citizen Number: 210602
target_id: abc, Moniker: C4sher, Citizen Number: 210602
target_id: abc, Moniker: C4sher, Citizen Number: 210602

除了上面的输出外,该脚本还创建了一个'output.json',其格式如下:

[{"Moniker": "C4sher", "target_id": "abc", "Citizen Number": "210602"}, 
  {"Moniker": "C4sher", "target_id": "abc", "Citizen Number": "210602"}, 
  {"Moniker": "C4sher", "target_id": "abc", "Citizen Number": "210602"}, 
  {"Moniker": "C4sher", "target_id": "abc", "Citizen Number": "210602"}, 
  {"Moniker": "C4sher", "target_id": "abc", "Citizen Number": "210602"}]

一些注意事项:

该脚本假定.py文件和'data.json'文件位于同一目录中。输入文件也被命名为'data.json'。我用“工作示例”更改问题的示例JSON:

[
  {
    "Handle": "abc"
  },
  {
    "Handle": "abc"
  },
  {
    "Handle": "abc"
  },
  {
    "Handle": "abc"
  },
  {
    "Handle": "abc"
  }
]