In 1 line of Python, get all json data from a webpage /

in #programming7 years ago (edited)
Hello steemit!

Ever regularly browsed websites that were set up with an API so that the community could download and work with json data locally? Didn't know how to set up your system to have it periodically be downloaded to your computer? Let's cut to the chase.

The line needs the packages json and urllib and goes like this:

with open('my_data.json', 'w') as outfile: json.dump(json.loads(urllib.urlopen('https://www.govtrack.us/data/congress/113/votes/2013/s11/data.json').read()), outfile)

If you think that's neat but unhandy, I agree. Let's take introduce some variables inbetween:

import json
import urllib

URL = 'https://www.govtrack.us/data/congress/113/votes/2013/s11/data.json'
# That's some example URL to json data found in the SO thread
# 19915010/python-beautiful-soup-how-to-json-decode-to-dict
# You find this format everywhere today

response = urllib.urlopen(URL)
raw_data = response.read()
data = json.loads(raw_data) # translate it to Python data
print(data)

filename = 'my_data.json'
with open(filename, 'w') as outfile:
    json.dump(data, outfile) # write Python data into a file


And that's that!


PS it's my third day here, so to be honest I'm still rather confused. Is there an intended choice for screen caps or is it rather random? I do posts and videos on math and programming. But if this toolbox Python stuff is valuable, let me know.


I'll leave you with some related tools:

import time
MIN = 60 # sixty seconds
for i in range(12*60*MIN):
    print(i)
    # <add some code here>
    time.sleep(10*MIN)

Execute some code every 10 minutes, for a day.

import time
unix_time = str(int(time.time()))

Get a unique common time stamp.

import os
dir_path_this = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path_this)

Write to where this script is located.


with open(filename) as infile:
    loaded_data = json.load(infile)

Turn your file content back to Python data.


You can also find this small script in this GitHub repository.

Take care!

Sort:  

looking forward to see more programming videos. we need this kind of STEM good content here! make sure that whatever you post is original and please link back to all the sources/references you use.

Good to hear, where does this interest focus lie here? I'm professionally working on algorithms, while there seems to be a lot of web-stuff here.

The interest here on Steemit? Definitely crypto.

Good, having seen but avoided the Python for a while, time for some action!

I liked your video example, very clear but I had to skip quite a few times since it went too slow for me. But I'm already a little bit familiar with the basics.

I'm just starting to learn Python, so I will be watching you :-)

I usually recommend the youtube series by MIT from 2008 on Introduction to programming, which uses Python! Have fun.

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.029
BTC 66066.52
ETH 3291.81
USDT 1.00
SBD 2.70