(Using Medium API Python Package)
If you need to retrieve a Medium article and save it in plain HTML format, this guide will walk you through the process using the Medium API in Python. This method allows you to get the article's content along with the associated styles. Before you start, ensure you have the `medium-api` library installed. You can install it using:
pip install medium-api
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.
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.
With your API key, you can create an instance of the `Medium` class. This object will be used to interact with the Medium API.
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.
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.