Engaging with the world around you has never been as important as it is today. In order to stay up-to-date, many of us interact with a wide range of news outlets via various platforms. But what if we could automate this process, fetching the top current news with a script? In this tutorial, we will show you how you can do exactly that using Python.
Table of contents
What Does Fetching Top News Mean?
To fetch the top news is to programmatically gather top-rated or popular news items from a particular source. This is typically done by accessing the source’s Application Programming Interface (API), which allows data transfers between different parts of a software or between different softwares.
Why Fetch News Using Python?
Python is one of the most preferred languages for web scraping and automation tasks due to its simplicity and the robustness of its libraries. With Python, you can pick up the top news items from your preferred news source at any given time automatically.
The Power of Automation
Automating the task of fetching top news not only saves time but also helps in maintaining regularity and consistency. Be it for staying informed, gathering data for research, or even developing your own news app, learning to fetch top news using Python can be a valuable addition to your coding skill set.
Whether you’re just beginning your coding journey or you’re an experienced developer, this tutorial is designed to get you up and running with news fetching in Python.
Fetching News with Python – Basic Setup
Before we dive into the code, we require an API key from a news service provider. In this case, we will be using the News API (newsapi.org). Once you’ve received your API key, we can start coding.
First, let’s install the necessary Python library, ‘requests’, which we’ll use to send HTTP requests and fetch news data. Open your terminal and type:
pip install requests
Now, let’s create a Python script named ‘fetch_news.py’ and import the ‘requests’ library.
import requests
Fetching the Top News
Our next step is to fetch the news using the ‘GET’ request. For this example, we’ll fetch the top headlines from ‘bbc-news’. Make sure to replace ‘your_api_key’ with the actual API key you received.
url = 'https://newsapi.org/v2/top-headlines?sources=bbc-news&apiKey=your_api_key' response = requests.get(url) print(response.json())
The API will respond with a JSON object, which consists of a success status, the total number of results, and an array of articles.
Working with the Response
Let’s now extract the individual news items from the response with Python’s in-built ‘json’ function:
data = response.json() articles = data["articles"] for article in articles: print(article['title'])
This prints out all the top headlines from BBC news.
Getting Specific News Items
We can also extract specific fields from each news item, such as the author, title, description, URL, etc.
for article in articles: print("Title: ", article['title']) print("Description: ", article['description']) print("URL: ", article['url'])
And just like that, with a few lines of Python, we can stay updated with the top news from our favorite source. Keep in mind that you can replace ‘bbc-news’ with your preferred news source’s identifier.
Filtering News by Keyword
Suppose we want to filter our news based on a keyword or topic. The code would then look like this:
keyword = "climate" url = "https://newsapi.org/v2/everything?q={0}&apiKey={1}".format(keyword, "your_api_key") response = requests.get(url) articles = response.json()["articles"] for article in articles: print("Title: ", article['title'])
This will fetch all news articles that have the keyword “climate”.
Filtering News by Date
We can also filter news based on the date of publishing. Here’s how to fetch news articles published on a specific date:
date = "2021-10-01" url = "https://newsapi.org/v2/everything?from={0}&sortBy=publishedAt&apiKey={1}".format(date, "your_api_key") response = requests.get(url) articles = response.json()["articles"] for article in articles: print("Title: ", article['title'])
This will fetch all news articles published on October 1, 2021.
Sorting News by Popularity
The News API also lets us sort the news by popularity, relevancy or recency. Here’s how to fetch news sorted by popularity:
url = "https://newsapi.org/v2/everything?sources=bbc-news&sortBy=popularity&apiKey=your_api_key" response = requests.get(url) articles = response.json()["articles"] for article in articles: print("Title: ", article['title'])
This will fetch top headlines from BBC news sorted by popularity.
Selecting a Language
To get news in a specific language, we can add the language parameter to the request URL:
url = "https://newsapi.org/v2/top-headlines?sources=bbc-news&language=en&apiKey=your_api_key" response = requests.get(url) articles = response.json()["articles"] for article in articles: print("Title: ", article['title'])
This will fetch top headlines from BBC news in English language.
Conclusion
Python provides powerful and easy-to-use tools for automating news fetch process. This not only saves time, but also opens up new possibilities for data analysis and app development. At Zenva, we believe that learning such skills can be a game-changer in your programming journey.
Where to Go Next?
Congratulations! Now that you know how to fetch news using Python, your journey with Python is just beginning. Naturally, the next question might be “where do I go from here?”
At Zenva, we encourage continual exploration of your newfound knowledge. Start playing around with this code – modify it, add features, and try to make it your own. By learning, executing then experimenting, you can cement the knowledge you’ve gained and turn it into practical experience.
One fantastic way to continue is to check out the wide range of courses we offer. A great starting point is our Python Mini-Degree.
Python Mini-Degree
The Python Mini-Degree we offer at Zenva Academy is a comprehensive collection of courses that teach Python programming, covering various topics such as coding basics, algorithms, object-oriented programming, game development, and app development. Students even learn by creating real-world apps, games and algorithms. While the courses are designed for beginners, they also cater to more experienced programmers.
What’s more, each course includes projects that you can add to your Python portfolio, showcasing your skills to potential employers in the high-demand field of Python programming, especially in data science.
Flexibility, Practicality, and Expertise at Zenva
What sets Zenva apart is our flexible learning system – our courses can be accessed anytime which means you can learn at your own pace. And to make sure that your learning is reinforced, we’ve included coding challenges and quizzes in our curriculum.
Our instructors are not only experienced but also certified by Unity Technologies and CompTIA. Furthermore, our courses have helped many learners land jobs, start their own businesses, and publish their own games and websites.
For a broader collection of Python courses, feel free to explore our Python Courses.
At Zenva, we believe in empowering you with practical skills so that you can go from beginner to professional on your own terms because when it comes to learning programming, the only limit is your own imagination.
Conclusion
With the simple act of fetching news using Python, we’ve introduced you to the vast potential of programming for time-saving automation and real-world application. It’s just the tip of the iceberg of what you can accomplish with Python, a language renowned for its clarity and versatility.
As you embark on your journey of exploring the countless ways in which Python can transform your problem-solving and create exciting solutions, remember that Zenva is here to mentor you every step of the way. Whichever coding path you choose, let our Python Mini-Degree be your compass, guiding you through the thrilling landscape of learning Python.