Function in Python to extract web Data I developed this feature that I think can be improved quite a bit. The result is the desired one, but it took me many lines of code. Any idea to optimize it?
import json
import urllib.request

dni = 71179047
    
def get_cuit(dni):
    request = urllib.request.Request('https://www.cuitonline.com/search.php?q=' + str(dni))
    response = urllib.request.urlopen(request)
    data_content = response.read()
    html = data_content.decode('ISO-8859-1')
    cuit = re.findall(r&quot;\(?\b[0-9]{2}\)?-?[0-9]{7,8}-[0-9]{1}\b&quot;, html)[0]
    result = re.sub('[^0-9]','', cuit)
    return result

get_cuit(dni)
Use Requests for HTTP and let it deal with the encoding
Do not bake the query into the URL string
Use BeautifulSoup for HTML parsing
Use typehints

Nice and simple:
from requests import get
from bs4 import BeautifulSoup


def get_cuit(dni: int) -&gt; str:
    with get(
        'https://www.cuitonline.com/search.php',
        params={'q': dni},
    ) as resp:
        resp.raise_for_status()
        doc = BeautifulSoup(resp.text, 'html.parser')

    span = doc.find('span', class_='cuit')
    return span.text


print(get_cuit(71_179_047))