gue*_*tli 2 python url-parsing
我有这样的网址:
https://user:password@example.com/path?key=value#hash
结果应为:
https://user:???@example.com/path?key=value#hash
我可以使用正则表达式,但是我想通过将url解析为高级数据结构,然后对该数据结构进行操作,然后序列化为字符串来“正确”地做到这一点。
Python可以做到吗?
请在拒绝投票之前发表评论。这个问题怎么了?
您可以使用内置urlparse查询器从URL中查询密码。它在Python 2和3中可用,但位置不同。
Python 2 import urlparse
Python 3 from urllib.parse import urlparse
例
from urllib.parse import urlparse
parsed = urlparse("https://user:password@example.com/path?key=value#hash")
parsed.password # 'password'
replaced = parsed._replace(netloc="{}:{}@{}".format(parsed.username, "???", parsed.hostname))
replaced.geturl() # 'https://user:???@example.com/path?key=value#hash'
另请参阅以下问题:更改URL中的主机名
| 归档时间: | 
 | 
| 查看次数: | 1209 次 | 
| 最近记录: |