Punycode domains doesn't work with requests

202 Views Asked by At

Is there an easy way to fetch punycode domains? I tried using the requests module, but it didn't work.

The following code doesn't work:

import requests
requests.get("https://.la")
InvalidURL: Failed to parse: https://.la

using Python 3.10.4, requests==2.28.1, urllib3==1.26.13

1

There are 1 best solutions below

0
On

I solved my problem using the following function

from urllib.parse import urlparse, urlunparse


def convert_idna_address(url: str) -> str:
    parsed_url = urlparse(url)
    return urlunparse(
        parsed_url._replace(netloc=parsed_url.netloc.encode("idna").decode("ascii"))
    )