如何将报废的Json数据保存到CSV或文本文件

时间:2019-03-01 13:33:48

标签: python web-scraping

我需要知道如何将抓取的数据保存到csv文件中,这是代码

[{'name': 'Absz', 'phone': '66343212'}, {'name': 'ddd ', 'phone': '545432211'}, {'name': 'ezd' 'phone':'54856886'}]

与输出类似

CSV

我想将所有数据存储在 public interface IOwner { int Owner { get; set; } } public interface IArmy { Army Army { get; set; } } public interface ITreasure { Treasure Treasure { get; set; } } public class Creep : IOwner, IArmy, ITreasure { public int Owner { get; set; } public Army Army { get; set; } public Treasure Treasure { get; set; } } 文件或文本文件

我该怎么做?

1 个答案:

答案 0 :(得分:2)

相当简单的任务。 您是否尝试过搜索,因为有大量将字典转换为csv文件的示例?

import requests
import json
import pandas as pd

parameters = ['a:1','a:2','a:3','a:4','a:3','a:4','a:5','a:6','a:7','a:8','a:9','a:10']

results = pd.DataFrame()
for item in parameters:
    key, value = item.split(':')
    url = "https://xxxx.000webhostapp.com/getNamesEnc02Motasel2.php?keyword=%s&type=2&limit=%s" %(key, value)
    try:        
        r = requests.get(url)
        cont = json.loads(r.content)
        temp_df = pd.DataFrame(cont)

        results = results.append(temp_df)
    except:
        continue

results.to_csv('path/to/filename.csv', index=False)