Filtering HTML with python

From Media Design: Networked & Lens-Based wiki
Revision as of 16:58, 23 May 2020 by Michael Murtaugh (talk | contribs) (Created page with "<source lang="python"> from urllib.parse import urljoin def absolute_hrefs(html, baseurl): t = html5lib.parseFragment(html, treebuilder = "etree", namespaceHTMLElements =...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
from urllib.parse import urljoin

def absolute_hrefs(html, baseurl):
    t = html5lib.parseFragment(html, treebuilder = "etree", namespaceHTMLElements = False)
    for a in t.findall(".//*[@href]"):
        linkclass = a.attrib.get("class", "")
        href = urljoin(baseurl, a.attrib.get("href"))
        a.attrib['href'] = href
    html = ET.tostring(t, method="html", encoding="unicode")
    return html