Medium to Markdown via Python: Ease of use
6 min readMar 15, 2024
If you write in Medium and also in a site that supports Markdown (Jekyll, Hugo), you probably have your own method to copy from one to the other. In one of my articles, I introduced a basic Python script to convert Medium to Markdown. In the last article, we polished it.
Now we’ll make it easier to use by automating the front matter and using input parameters.
Recap
The script, after the last articles, looks like this:
from datetime import datetime
import html2text
import requests
from bs4 import BeautifulSoup
import sys
import re
def get_html_element(element,soup) -> str:
"""
Searches for the first occurrence of a specified HTML element in a BeautifulSoup object and returns its text.
Parameters:
- element (str): The tag name of the HTML element to search for (e.g., 'h1', 'div').
- soup (BeautifulSoup): A BeautifulSoup object containing the parsed HTML document.
Returns:
- str: The text of the first occurrence of the specified element if found; otherwise, an empty string.
"""
result = soup.find(element)
if result:
return result.text
else:
print(f"No element ${element} found.")
return ""
def cut_text_at_marker(marker:str,text:str,beginning:bool):
"""
Cuts the text at the specified marker and returns the resulting…