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

前端 未结 4 1070
旧时难觅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 11:01

    If you want to know if an URL is absolute or relative in order to join it with a base URL, I usually do urlparse.urljoin anyway:

    >>> from urlparse import urljoin
    >>> urljoin('http://example.com/', 'http://example.com/picture.png')
    'http://example.com/picture.png'
    >>> urljoin('http://example1.com/', '/picture.png')
    'http://example1.com/picture.png'
    >>> 
    

提交回复
热议问题