是否可以有一个没有方案的绝对 href 属性?

Is it possible to have an absolute href attribute with no scheme?

我正在尝试找到一种方法来测试 URL 在 Python 中是绝对的还是相对的。在 HTML 标签的 href 属性中,是否缺少足以将 URL 标记为相对的方案(例如 http、ftp 等),或者是否有可能绝对 URL 作为 href 属性而不明确指定方案(例如 'www.google.com')?我正在使用 urlparse.urlparse('some url').scheme.

获取方案

如果您不包含 URI 方案(http://https://// 等),则浏览器将假定它是相对的 URL .

您应该了解方案相关的 URL,例如您的脚本的 //www.google.com。简而言之,您应该寻找双正斜杠 // 以确定 URL 是否将被视为相对。

根据RFC 3986

<a href="//example.com/page.html">Link</a>

来源和更多信息:

在python中:

$ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
>>> from urlparse import urlparse;
>>> urlparse('//example.com/page.html');
ParseResult(scheme='', netloc='example.com', path='/page.html', params='', query='', fragment='')