Medium to Markdown via Python: Polish

Danielle H
6 min readMar 8, 2024

If you write in Medium and also in a site that supports Markdown, you probably have your own method to copy from one to the other. In my last article, I introduced a basic Python script to convert Medium to Markdown. Now we’ll add some polish.

Polish the code (generated by Dall-E)

Check out the full code in GitHub here.

In the last article, we created the following script:

from datetime import datetime
import html2text
import requests
from bs4 import BeautifulSoup
import sys

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 ""

### get html content from url
url = "https://medium.com/@dsavir-h/flood-of-tears-2edc7bcf306b"
response = requests.get(url)
html_content = response.text

### define soup
soup = BeautifulSoup(html_content, 'lxml')

### get title
title = get_html_element('h1',soup)…

--

--

Danielle H

I started programming in LabVIEW and Matlab, and quickly expanded to include Android, Swift, Flutter, Web(PHP, HTML, Javascript), Arduino and Processing.