Python3的URL解析库urlparse

516次阅读
没有评论

Python3的URL解析库urlparse,希望可以帮助到大家。

Python3的URL解析库urlparse

解析一个 URL 获得各个概念所对应的值在 Python 中显得很简单,

Python3 中将 urllib2、urlparse 和 robotparse 并入了 urllib 模块中,

所以原本在 Python 导入的方式在 Python3 中应该这样导入:

from urllib.parse import urlparse

使用它我们可以获得 ParseResult 对象,

我们可以通过下标或者属性名来访问对象属性:

  • scheme (协议)
  • netloc (域名)
  • path (路径)
  • params (可选参数)
  • query (连接键值对)
  • fragment (特殊锚)

Python3的URL解析库urlparse

我们测试下这个函数的使用:

#!/usr/bin/env python
# _*_ Coding: UTF-8 _*_
from urllib.parse import urlparse
result = urlparse('https://juejin.im/user/2805609406139950/posts?params=123&username=123')
print(result)

输出的结果是:

ParseResult(
    scheme='https', 
    netloc='juejin.im', 
    path='/user/5da32395e51d4578200cc9c5/posts', 
    params='', 
    query='params=123&username=123', 
    fragment=''
)
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-06-10发表,共计661字。
新手QQ群:570568346,欢迎进群讨论 Python51学习