KeyError in JSON request Python - NYT API
<p>I am trying to extract the URL of specific articles from NYT API. 
This is my code:</p>

<pre><code>import requests

for i in range(0,100):
    page=str(i)
    r = requests.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?begin_date=20100101&amp;q=terrorist+attack&amp;page="+page+"&amp;api-key=***")
    data = r.json()
    article = data['response']['docs']   
    for url in article:
        print(url["web_url"])
</code></pre>

<p>After printing the first 20 URL it gives me this error</p>

<pre><code>KeyError: 'response'
</code></pre>

<p>however by checking random pages the key 'response' is present in any of them. What can I do to print all the URLs from the next 88 pages?</p>

<p>You are assuming that there are at least 101 pages to make requests (0 to 100).</p>

<p>If you make a request to page 100, do you still get the same JSON structure with a <code>response</code> key?</p>

<p>What you should instead use is a while loop that breaks when you get a <code>KeyError</code>.</p>

python