(Using Medium API Python Package)
In this guide, we'll walk through how to retrieve recommended feed
articles from Medium using the Medium API in Python. By following these
steps, you'll be able to fetch and display articles based on a specific
tag. Before you start, make sure you have the medium-api
library
installed. If not, you can install it using the following command:
pip install medium-api
Let's break down the code and understand each part.
First, we need to import the necessary libraries. Here, we'll use the os
library to handle environment variables and the Medium
class from the medium_api
library to interact with Medium's API.
Next, we retrieve the API key from the environment variables. The API key is necessary to authenticate and make requests to Medium's API.
With the API key, we can now create an instance of the Medium
class. This object will be used to interact with Medium's API.
To get the recommended feed, we create a RecommendedFeed
object by specifying a tag (e.g., "artificial-intelligence") and the number of articles we want to fetch (e.g., 15). After creating the object, we call the fetch_articles()
method to retrieve the articles.
Finally, we loop through the fetched articles and print their titles.
The output of the script will display the titles of the recommended articles. For example:
By following these steps, you will be able to fetch and display recommended articles from Medium based on a specific tag. Feel free to adjust the tag and count parameters according to your needs.