Skip to content

Instantly share code, notes, and snippets.

@drhanlau
Created January 26, 2020 01:40
Show Gist options
  • Save drhanlau/bcdf9fbf2921601e26c2560630ace224 to your computer and use it in GitHub Desktop.
Save drhanlau/bcdf9fbf2921601e26c2560630ace224 to your computer and use it in GitHub Desktop.
Port news from API to DB
import urllib.request, json
with urllib.request.urlopen("https://us-central1-newsproject-41eb3.cloudfunctions.net/getLatestCoronaNews") as url:
data = json.loads(url.read().decode())
news = data['data'][0]['data']
query = "INSERT INTO news (url, title, description, content, published, imageUrl) VALUES (%s, %s, %s, %s, %s, %s)"
for n in news:
t = (n['url'], n['title'],n['description'],n['content'], n['publishedAt'], n['urlToImage'])
print(t)
mycursor.execute(query, t)
mydb.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment