Created
October 13, 2022 13:51
-
-
Save bamford/c399447ebc8e085b823ef49f81428b57 to your computer and use it in GitHub Desktop.
Formatting Moodle forum posts from html export
This file contains 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
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "from bs4 import BeautifulSoup", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "html = \"\".join(open(\"discussion.html\").readlines())\n\nsoup = BeautifulSoup(html)\nnewsoup = BeautifulSoup()\n\nrows = soup.find(\"table\").find_all(\"tr\")\n\nfor row in rows:\n cells = row.find_all(\"td\")\n if cells:\n title, content = cells[8:10]\n title.name = \"h3\"\n newsoup.append(newsoup.new_tag(\"hr\", style=\"border-width: 3px\"))\n newsoup.append(title)\n content.name = \"div\"\n for tag in content.find_all(\"h3\"):\n tag.name = \"h4\"\n newsoup.append(content)", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "newsoup", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "conda-env-first-year-new-py", | |
"display_name": "Python [conda env:first-year-new]", | |
"language": "python" | |
}, | |
"hide_input": false, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.10.4", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "Formatting Moodle forum posts from html export", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment