0

Extract Text from HTML using a CSS Selector

by
Published May 26, 2023

This script is a simple text extractor from a valid HTML string. You should specify a css selector, such as ".content", "#id", etc. You will need to obtain the HTML text through another integration, e.g. the HTTP request script of this integration

Script http Verified

The script

Submitted by marco lussetti774 Python3
Verified 1260 days ago
1
# import wmill
2
from bs4 import BeautifulSoup
3

4

5
def main(html: str, css_selector: str = ""):
6
    if not css_selector:
7
        return {"text": html}
8
    else:
9
        soup = BeautifulSoup(html, "html.parser")
10
        matches = [el.get_text() for el in soup.select(css_selector)]
11
        return {"text": "\n\n".join(matches), "matches": len(matches)}
12