-1

When I try and run this program it always shows KeyError and if I keep a try/except block no information gets printed. The error is generated at submission_dict at title as keyerror. at 16th line

from operator import itemgetter
import requests

#Make an API call and store the response
url='https://hacker-news.firebaseio.com/v0/topstories.json'
r=requests.get(url)
print(f"Status Code:{r.status_code}")

#Process the information about each submission
submission_ids=r.json()
submission_dicts=[]
for submission_id in submission_ids[:30]:
    #Make a seperate api call for each id
    url1=f"https://hacker-news.firebaseio.com/v0/item{submission_id}.json"
    r1=requests.get(url1)
    response_dict=r1.json()
    submission_dict={
            'title':response_dict['title'],
            'hn_link':f"http://news.ycombinator.com/item?id={submission_id}",
            'comments':response_dict['descendants']
        }
    submission_dicts.append(submission_dict)
submission_dicts=sorted(submission_dicts,key=itemgetter('comments'),reverse=True)
for submission_dict in submission_dicts:
    print(f"\nTitle: {submission_dicts['title']}")
    print(f"\nDiscuission link: {submission_dicts['hn_link']}")
    print(f"\nComments: {submission_dicts['comments']}")
<ipython-input-11-956f08dd0f07> in <module>
     15         print(f"id: {submission_id}\tstatus: {r.status_code}")
     16         submission_dict={
---> 17                 'title':response_dict['title'],
     18                 'hn_link':f"http://news.ycombinator.com/item?id={submission_id}",
     19                 'comments':response_dict['descendants']```
  • Can you update your question to include the exact error message you are getting? – Matt Feb 24 '22 at 11:49
  • Please avoid posting images (or worse, links to images) of code or errors. Anything text-based (code and errors) should be posted as text directly in the question itself and formatted properly as a [mre]. You can get more [formatting help here](https://stackoverflow.com/help/formatting). You can also read about [why you shouldn't post images/links of code](//meta.stackoverflow.com/q/285551). – Tomerikoo Feb 24 '22 at 12:20
  • Please post the full text of the error. Everything from `Traceback: most recent...` and to the end. Just not as an image... – Tomerikoo Feb 24 '22 at 12:31

1 Answers1

0
from operator import itemgetter
import requests

#Make an API call and store the response
url='https://hacker-news.firebaseio.com/v0/topstories.json'
r=requests.get(url)
print(f"Status Code:{r.status_code}")

#Process the information about each submission
submission_ids=r.json()
submission_dicts=[]
for submission_id in submission_ids[:3]:
    #Make a seperate api call for each id
    url1=f"https://hacker-news.firebaseio.com/v0/item/{submission_id}.json"
    r1=requests.get(url1)
    response_dict=r1.json()
    submission_dict={
            'title':response_dict['title'],
            'hn_link':f"http://news.ycombinator.com/item?id={submission_id}",
            'comments':response_dict['descendants']
        }
    submission_dicts.append(submission_dict)
submission_dicts=sorted(submission_dicts,key=itemgetter('comments'),reverse=True)
for submission_dict in submission_dicts:
    print(f"\nTitle: {submission_dict['title']}")
    print(f"\nDiscuission link: {submission_dict['hn_link']}")
    print(f"\nComments: {submission_dict['comments']}")
Deven Ramani
  • 751
  • 4
  • 10
  • Here response_dict is a dictionary returned by the url and, hence i don't see how response_dict has no key. I've also tried try catch and putting it that way shows all the link still has no key and prints the message of except block and its kind of faulty interpretation. I am confused. – sarthak basnet Feb 24 '22 at 15:38
  • there's a plenty of typos in your code, please check my edit updated code which work fine – Deven Ramani Feb 25 '22 at 06:17