Search code examples
pythonnewsapi

How can I request the first 100 titles in NewsAPI instead of first one?


Here, the code returns the first article's title according to the search information

import requests

r = requests.get("https://newsapi.org/v2/everything?qInTitle=bangladesh&from=2023-07-17&to=2023-08-16&sortBy=popularity&language=en&apiKey=45c28deca60947fd9ec4d8db2b2c4a81")
content = r.json()

print(content['articles'][0]['title'])

How can I make it so that it returns the first 100 or something similar?

I've tried to remove the [0] and putting [0:100] instead but it didn't work; I am new to coding I apologize.


Solution

  • You can do like this:

    import requests
    
    r = requests.get("https://newsapi.org/v2/everything?qInTitle=bangladesh&from=2023-07-17&to=2023-08-16&sortBy=popularity&language=en&apiKey=45c28deca60947fd9ec4d8db2b2c4a81")
    content = r.json()
    
    for row in content['articles'][:100]:
      print(row['title'])