如何将抓取的 Json 数据保存到 CSV 或文本文件 [英] How to Save Scraped Json Data to CSV or Text File

查看:65
本文介绍了如何将抓取的 Json 数据保存到 CSV 或文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i need to know how to save scraped data into a csv file this is the code

import requests
import json

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',]

for item in parameters:
    key, value = item.split(':')[0], item.split(':')[1]
    url = "https://xxxx.000webhostapp.com/getNamesEnc02Motasel2.php?keyword=%s&type=2&limit=%s" %(key, value)
    r = requests.get(url)
    cont = json.loads(r.content)
    print(cont)

和输出一样

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

我想将所有数据存储在 CSV 文件或文本文件中

I want to store all data in CSV File or Text File

我该怎么做?

推荐答案

相当简单的任务.你没有尝试搜索这个,因为有大量将字典转换为 csv 文件的例子吗?

Rather simple task. Did you not try to search this, as there are an abundance of examples of turning a dictionary into a csv file?

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)

这篇关于如何将抓取的 Json 数据保存到 CSV 或文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆