How to Save a Medium Article in Clean HTML Format

(Using Medium API Python Package)

Wanna retrieve a Medium article and save it in clean HTML format? Want to personalize the article's style with custom CSS to meet your brand's identity?

This guide will walk you through this exact process using the Medium API in Python. This method allows you to get the article's content in clean HTML, along with the associated styles (which you can edit later).

Before we start, ensure you have the medium-api library installed. You can install it using:

pip install medium-api

Full Code


Step-by-Step Guide

1. Import Libraries

First, you need to import the required libraries. In this case, you'll use `os` to access environment variables and `Medium` from the `medium_api` library to interact with Medium's API.

2. Retrieve the API Key

Medium API requires an API key for authentication. You'll need to retrieve this key from your environment variables. Make sure you have set the environment variable `RAPIDAPI_KEY` with your Medium API key.

3. Create a Medium Object

With your API key, you can create an instance of the `Medium` class. This object will be used to interact with the Medium API.

4. Get the Article

To retrieve a specific article, use the `article` method of the `Medium` object. You need to provide the unique `article_id` of the Medium article you wish to fetch.

5. Save and Print the Article's HTML

Finally, you can save the article's HTML. You have the option to save it as a full webpage including a CSS stylesheet. The `fullpage` parameter, when set to `True`, ensures that the HTML includes the complete page structure. You can also specify a URL for the CSS stylesheet.

This code snippet will save the article's HTML content to a file and print it. The `save_html` function allows you to choose whether to include the full webpage or just the raw HTML content, along with applying a CSS stylesheet for styling.

By following these steps, you can easily fetch and save any Medium article in HTML format with the desired styling.

nishu@mediumapi.com