我需要知道如何将抓取的数据保存到 csv 文件中,这是代码import requestsimport jsonparameters = ['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文件或文本文件中我怎么能做到这一点?
1 回答
牧羊人nacy
TA贡献1862条经验 获得超7个赞
比较简单的任务。你没有尝试搜索这个,因为有很多将字典转换为 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)
添加回答
举报
0/150
提交
取消