How can I check if a URL is absolute using Python?

前端 未结 4 1061
旧时难觅i
旧时难觅i 2020-12-10 10:20

What is the preferred solution for checking if an URL is relative or absolute?

4条回答
  •  無奈伤痛
    2020-12-10 10:40

    Can't comment accepted answer, so write this comment as new answer: IMO checking scheme in accepted answer ( bool(urlparse.urlparse(url).scheme) ) is not really good idea because of http://example.com/file.jpg, https://example.com/file.jpg and //example.com/file.jpg are absolute urls but in last case we get scheme = ''

    I use this code:

    is_absolute = True if '//' in my_url else False

提交回复
热议问题