解析url并从中获取所有不同的部分
url-parser的Python项目详细描述
Python URL解析器
这是一个很好的包,可以帮助您用vanilla python解析所有类型的URL,并将解析后的URL分组返回。
为了不停止API parse_url
(返回dict)仍然有效,我们让get_url
将url部分作为对象获取。在
在版本2.1中,我们还包含了get_basic_url
一个小而简洁的函数,可以从字符串中获取主url
安装
pip install url-parser
用法
^{pr2}$关键词get_url
和parse_url
当使用parse_url
和get_url
函数时,您将得到一个dict(parse_url)或一个对象(get_url),其中包含url的不同部分。在
可以通过关键字访问不同的部分:
对于parse_url
使用:result['top_domain]
对于get_url
使用:result.top_domain
以下是所有可用关键字的列表:
Keyword | Desription | Value when not present in URL |
---|---|---|
protocol | The protocol, e.g. https or ftp | None |
www | Returns www if www is used in the URL | None |
sub_domain | The sub domain, e.g. my.subdomain in my.subdomain.example.com. Note that the sub domain also includes www. | None |
domain | The domain, e.g. example in example.com | Is always present |
top_domain | The domain, e.g. com in example.com | Is always present |
dir | The directory, e.g. /my/directory/ in example.com/my/directory/ | None |
file | The file, e.g. my_file.js in example.com/home/my_file.js | None |
path | The full path, e.g. /home/my_file.js in example.com/home/my_file.js | None |
fragment | The URL fragment, e.g. my_link in example.com#my_link | None |
query | The URL query, e.g. my_parameter=1&foo=bar in example.com?my_parameter=1&foo=bar | None |
测试
使用以下命令运行测试。在
python -m unittest url_parser.tests.test_url_parser
变更日志:
看到了吗变更日志.md在
- 项目
标签: