[Python] Validate urls with anchor hashbangs in python

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to check if a url with section anchors using the # operator (example: http://url_link.html#section) is valid in Python.

    I'm using urlopen to check as below:

    from urllib.request import urlopen

    def is_valid_url(url):
    try:
    r = urlopen(url)
    return r.status == 200
    catch Exception e:
    return False


    But calling is_valid_url with invalid section anchors doesn't return False

    is_valid_url("http://valid_url_link.html#valid_section") # True
    is_valid_url("http://valid_url_link.html#invalid_section") # Also True!


    Is there a way to detect that http://valid_url_link.html#invalid_section is not a valid url in Python?

    Continue reading...

Compartilhe esta Página