Medium to Markdown via Python: the basics
How to create a python script for your conversion needs
If you write in one place, e.g. Medium, maybe you write in another place, such as your own Jekyll or Hugo site. But why write everything twice? Take the time to craft a Python script that fits your needs exactly, and then sit back and enjoy the extra time to do interesting things :)
Getting the HTML content
First, we need to get the Medium article as HTML. Note that this means you need to publish your article to Medium before converting it, this will not work with a draft (though that’s on my to-do list).
Then we use the excellent requests library:
import requests
url = "https://medium.com/@dsavir-h/flood-of-tears-2edc7bcf306b"
### get html content from url
response = requests.get(url)
# you can check the response.status_code first if you like (see comment)
html_content = response.text
This is simple: Get request the HTML content from the URL, and return the text.
You can also check the response_code and make sure it’s 200, but Medium redirects to a custom 404 page if the page is not found (while returning 200 as a response) so it doesn’t really make a difference.