six.moves.urllib.parseurljoin()方法的完全解析和使用方式
urllib.parse.urljoin() 方法用于拼接URL,它接受一个基础URL和一个相对URL作为参数,返回一个完整的URL。
完整的用法是:
urllib.parse.urljoin(base, url, allow_fragments=True)
基础URL是指用来解析相对URL的参考点,相对URL是指不包含域名的URL(如:/images/img.jpg)。allow_fragments 参数的默认值为 True,表示支持拼接锚点(即 URL 中 # 后面的部分),如果设置为 False,则不支持。
下面是一个使用 urllib.parse.urljoin() 方法的例子:
from urllib.parse import urljoin base_url = "https://www.example.com/" relative_url = "/images/img.jpg" full_url = urljoin(base_url, relative_url) print(full_url)
运行上面的代码,输出的结果是:
https://www.example.com/images/img.jpg
解析过程如下:
- 基础URL为 "https://www.example.com/"
- 相对URL为 "/images/img.jpg"
- 将相对URL拼接到基础URL上得到最终的URL "https://www.example.com/images/img.jpg"
如果基础URL中已经包含路径部分(如:"https://www.example.com/images/"),那么相对URL将会覆盖掉基础URL中的路径部分(如:"/images/img.jpg")。
base_url = "https://www.example.com/images/" relative_url = "/docs/doc.pdf" full_url = urljoin(base_url, relative_url) print(full_url)
运行上面的代码,输出的结果是:
https://www.example.com/docs/doc.pdf
解析过程如下:
- 基础URL为 "https://www.example.com/images/"
- 相对URL为 "/docs/doc.pdf"
- 将相对URL拼接到基础URL上得到最终的URL "https://www.example.com/docs/doc.pdf"
总结一下,urllib.parse.urljoin() 方法可以方便地拼接URL,特别适合用于处理相对URL和基础URL之间的关系。
