Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - Pyton Requests to CSV

I've been trying to get the extract a JSON requests API result into a csv but I'm struggeling,

so far the below code return the value in the terminal

import requests
import csv
import json
from pprint import pprint
import urllib
import pandas as pd

url = "https://esi.evetech.net/latest/markets/10000002/orders/?datasource=tranquility&order_type=all&page=1"

payload={}
headers = {}
r = {}

response = urllib.request.urlopen(url)

text = response.read()
json_data = json.loads(text)

pprint(json_data)
df = pd.read_json(r)
df.to_csv("output.csv")

however Pandas return me with an error in regards of the Dictionary class? ( apologize I'm not really familiar when it come to coding )

Last question would be, what would be the logic to continue the url request beyond page=1 in the url variable? (order_type=all&page=1 ) I don't know how many page the system have

thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error is reproducible with the code above and looks like some encoding error with theurlib library. But I was able to load the data successfully using the python requests module

import requests
url = "https://esi.evetech.net/latest/markets/10000002/orders/datasource=tranquility&order_type=all&page=1"
response = requests.get(url)
data = response.text
df = pd.read_json(data)
df.to_csv("output.csv")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...