Python Forum
Code to retrieve data from a website
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code to retrieve data from a website
#1
Hi guys, I am trying to retrieve some data from the following link: https://quickstats.nass.usda.gov
I am somehow stuck on getting it as what I want to do is to download it automatically through a series of API codes and then export it to an excel. Could you maybe go through it and help me? thanks a lot.

import json
import pandas as pd
import requests

api_key = "API KEY AQUI"

commo_to_items = {
"SOYBEANS": ["SOYBEANS - YIELD, MEASURED IN BU / ACRE", "SOYBEANS - ACRES PLANTED", "SOYBEANS - ACRES HARVESTED", "SOYBEANS - PRODUCTION, MEASURED IN BU"],
"CORN": ["CORN, GRAIN - YIELD, MEASURED IN BU / ACRE", "CORN - ACRES PLANTED", "CORN, GRAIN - ACRES HARVESTED", "CORN, GRAIN - PRODUCTION, MEASURED IN BU"],
}

def get_data(commo, items, year):
data = {
"key": api_key,
"source_desc": "SURVEY",
"commodity_desc": commo,
"year": year,
"short_desc": items,
"agg_level_desc": "COUNTY",
"format": "JSON"
}

r = requests.get("http://quickstats.nass.usda.gov/api/api_GET/", params=data)
try:
df = pd.DataFrame(json.loads(r.text)["data"])[["county_name", "year", "statisticcat_desc", "location_desc", "unit_desc", "state_name", "commodity_desc", "Value"]]
df = df.rename(columns={
"county_name": "county",
"state_name": "state",
"location_desc": "location",
"unit_desc": "unit",
"commodity_desc": "commodity",
"Value": "value"
})
return df
except Exception as e:
print(commo)
print(e)
print(r.content)
return None

dfs = []
for year in range(2019, 2021):
print(year)
for commo, items in commo_to_items.items():
df = get_data(commo, items, year)
if df is not None:
dfs.append(df)

df = pd.concat(dfs)
df.to_csv("excel.csv",index=False)

BTW: the API KEY comes from the website and its a code that you need to get by registering within the website
Larz60+ write Jul-07-2022, 09:57 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

You will need to fix indentation first.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scraping Data from Website melkaray 3 791 Sep-22-2023, 12:41 PM
Last Post: melkaray
  Is this possible to write a script for checking data from website? WanW 2 1,123 Jun-02-2022, 02:31 AM
Last Post: Larz60+
  Retrieve data from oscilloscope using pyserial on ubuntu jte350 3 2,841 Apr-29-2021, 04:36 PM
Last Post: jte350
  Extracting data from a website tgottsc1 2 2,276 Jan-09-2021, 08:14 PM
Last Post: tgottsc1
  Retrieve data from MongoDB dsuk 2 2,303 Aug-24-2020, 09:22 PM
Last Post: micseydel
  Monitor specific line of code from website Olimpiarob 1 1,850 Jul-09-2020, 03:20 PM
Last Post: mrdominikku
  Regex to retrieve data from json in script tag. DreamingInsanity 4 9,596 Dec-20-2019, 06:18 PM
Last Post: DreamingInsanity
  Retrieve data from Terminal output or from an ASE variable. DreamingInsanity 0 2,219 Jul-08-2018, 05:51 PM
Last Post: DreamingInsanity
  extracting data from json on website larry2311 2 5,082 Feb-09-2018, 01:27 AM
Last Post: larry2311
  header of website but no data Brian1210 2 3,944 Oct-07-2017, 11:59 PM
Last Post: Brian1210

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020