Python xml custom scraper

python xml scraper

hi, is there any way to run a full-fledged python script in HA? I have a germ of an xml scraper here, which normally works, for example, on Linux Debian. can it be run here too?

import requests
from lxml import etree


login_url = "http://10.55.254.5"
data_url = "http://10.55.254.5/PAGE130.XML"


username = "*****"
password = "*****"


login_data = {
    "USER": username,
    "PASS": password
}


with requests.Session() as session:
    response = session.post(login_url, data=login_data)
    if response.status_code == 200:

        data_response = session.get(data_url)

        if data_response.status_code == 200:

            xml_content = data_response.content


            xml_tree = etree.fromstring(xml_content)


            data = xml_tree.xpath("//*")  


            for element in data:
                print(etree.tostring(element, encoding="unicode"))

        else:
            print("Error:", data_response.status_code)

    else:
        print("Error:", response.status_code)

Yes, you can create a command line sensor. Put your script somewhere under config, then the command for your sensor will be something like

command: 'python3 /config/path/to/your/script.py'

You didn’t mention what sort of data this is, but it looks like there might be many elements output? You might consider formatting the output into json, then you can save the data as an attribute of the sensor using json_attributes. Otherwise, you might run in to issues with the max length restriction for an entity state.

thank you, but there is a problem when I write the command python3 /config/path/to/your/script.py’ in the HA command line, it says that the command was not found.
I have no idea how to install it there

apk add python3
apk add py3-pip