Created
April 23, 2019 06:16
-
-
Save NISH1001/8ed79739eee37bd42cfb7af033e8874d to your computer and use it in GitHub Desktop.
convert medium blog post into markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import html2text | |
| import requests | |
| from bs4 import BeautifulSoup | |
| class ManualError(Exception): | |
| def __init__(self, args): | |
| self.args = args | |
| def display(self): | |
| print(''.join(self.args)) | |
| def markdownify(url): | |
| response = requests.get(url) | |
| try: | |
| if response.status_code != 200: | |
| raise ManualError("not good :/") | |
| else: | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| root = soup.find("div", {'class' : 'section-content'}) | |
| mark = html2text.html2text(root.prettify()) | |
| return mark | |
| except ManualError as merr: | |
| merr.display() | |
| return None | |
| def main(): | |
| url = "https://medium.com/@NIshanPantha/tetris-effect-beats-rhythms-and-music-4792bb1cb18#.rghv7b4q5" | |
| url1 = "https://medium.com/@NIshanPantha/tetris-effect-beats-rhythms-and-music" | |
| mark = markdownify(url) | |
| if mark: | |
| with open("test.md", "w") as fp: | |
| fp.write(mark) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment