Edits history of script submission #374 for ' Extract Text from HTML using a CSS Selector (http)'

  • python3
    One script reply has been approved by the moderators
    Ap­pro­ved
    # import wmill
    from bs4 import BeautifulSoup
    
    
    def main(html: str, css_selector: str = ""):
        if not css_selector:
            return {"text": html}
        else:
            soup = BeautifulSoup(html, "html.parser")
            matches = [el.get_text() for el in soup.select(css_selector)]
            return {"text": "\n\n".join(matches), "matches": len(matches)}
    

    Submitted by marco lussetti774 1098 days ago