Aviv
Aviv

Reputation: 21

Punycode domains doesn't work with requests

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

Upvotes: 1

Views: 285

Answers (1)

Aviv
Aviv

Reputation: 21

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"))
    )

Upvotes: 1

Related Questions