Save imported data as an Excel

Asked 2 years ago, Updated 2 years ago, 34 views

Hi, everyone While studying Open API, I made a code and tried to save it as Excel As far as I know, it is usually stored using the to_excel function I'm asking if I have to make a separate data frame for the data that I brought in because the coding is not good!

The code is as follows:

import requests import datetime import pandas as pd

url = "http://www.khoa.go.kr/oceangrid/grid/api/tideObsPreTab/search.do"

headers = { 'cache-control': "no-cache", 'postman-token': "51b374f3-ba0d-0077-1f3c-ff95b65a81ba" }

dt_index = pd.date_range(start='20200922', end='20200923') dt_list = dt_index.strftime("%Y%m%d").tolist()

for i in dt_list: date=i querystring = {"ServiceKey":"OHVMK3dF0Ah6K/oIG71f0g==","ObsCode":"DT_0001","Date":date,"ResultType":"json"} response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)

response.to_excel('testt.xlsx', index=False)

api

2022-09-20 20:02

1 Answers

response.to_excel I'm saving it like this.

response is a requests.Response object, and there is no method to_excel in this class. Usually, response is changed to pandas.DataFrame and is saved by running to_excel on that object.

So if you fix the code, it's roughly as follows.

import requests
import datetime
import pandas as pd


url = "http://www.khoa.go.kr/oceangrid/grid/api/tideObsPreTab/search.do"

headers = {"cache-control": "no-cache", "postman-token": "51b374f3-ba0d-0077-1f3c-ff95b65a81ba"}

dt_index = pd.date_range(start="20200922", end="20200923")
dt_list = dt_index.strftime("%Y%m%d").tolist()

data = []
for i in dt_list:
    date = i
    querystring = {
        "ServiceKey": "#######################",
        "ObsCode": "DT_0001",
        "Date": date,
        "ResultType": "json",
    }
    response = requests.request("GET", url, headers=headers, params=querystring)
    print(response.text)

    data.extend(response.json()["result"]["data"])

df = pd.DataFrame(data)
print(df)
df.to_excel("testt.xlsx", index=False)


2022-09-20 20:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.