Created
June 14, 2024 16:52
-
-
Save maciejskorski/048696c8914d530b30ed7d53e0703464 to your computer and use it in GitHub Desktop.
How to use The Guardian API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from functools import partial | |
API_KEY = os.getenv('API_KEY') # NOTE: can use the limited 'test' key for demo purposes | |
QUERY_TEMPLATE = 'https://content.guardianapis.com/{section}/{year}/{month}/{day}/{title}?api-key={API_KEY}&show-blocks=all' # NOTE: can try less info, e.g. &show-fields=BodyText | |
QUERY_FN = partial(QUERY_TEMPLATE.format,API_KEY=API_KEY) | |
web_url = 'https://www.theguardian.com/business/2014/sep/10/thorntons-60-per-cent-profits-rise-despite-closures' # NOTE: put your desired url | |
section, year, month, day, title = web_url.split('/')[3:] | |
query = QUERY_FN(section=section,year=year,month=month,day=day,title=title) | |
results = requests.get(query).json() # content inside |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment