在Python中解析mailto url
我正在尝试将 mailto URL 解析为一个不错的对象或字典,其中包括 subject
、body
等。我似乎找不到实现此目的的库或类- 你知道吗?
mailto:[email protected]?subject=mysubject&body=mybody
I'm trying to parse mailto URLs into a nice object or dictionary which includes subject
, body
, etc. I can't seem to find a library or class that achieves this- Do you know of any?
mailto:[email protected]?subject=mysubject&body=mybody
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 urlparse 和 parse_qs 来解析以 mailto 作为方案的 url。请注意,根据方案定义:
相同:
与以下示例
You can use urlparse and parse_qs to parse urls with mailto as scheme. Be aware though that according to scheme definition:
is identical to
Here's an example:
核心 urlparse 库在 mailtos 上的表现并不出色,但已经完成了一半:
编辑
一些研究发现 此线程。底线:python url 解析很糟糕。
The core urlparse lib does less than a stellar job on mailtos, but gets you halfway there:
EDIT
A little research unearths this thread. Bottom line: python url parsing sucks.
看起来您可能只想编写自己的函数来执行此操作。
编辑:
这是一个示例函数(由 python noob 编写)。
编辑2,清理反馈:
Seems like you might just want to write your own function to do this.
Edit:
Here is a sample function (written by a python noob).
Edit 2, cleanup do to feedback:
包括电池:urlparse。
Batteries included: urlparse.
这是使用 re 模块的解决方案...
这假设它的格式与您发布的格式相同。您可能需要进行修改以更好地满足您的需求。
Here is a solution using the re module...
This assumes it is in the same format as you posted. You may need to make modifications to better fit your needs.
你应该使用像这样的特殊库
https://pypi.python.org/pypi/urlinfo
和贡献并创建问题以使 Python 变得更好;)
PS 不使用 Robbert Peters 解决方案 bcz 它被破解并且无法正常工作。同样使用正则表达式的是使用超级BFG枪来获得小鸟。
You shold use special library like that
https://pypi.python.org/pypi/urlinfo
and contribute and create issue to make Python better ;)
P.S. Does not use Robbert Peters solution bcz it hack and does not work properly. Also using a regular expression is using super BFG Gun to get small bird.
我喜欢 Alexander 的答案,但它是用 Python 2 编写的!现在,我们从
urllib.parse
获取urlparse()
和parse_qs()
。另请注意,对标头进行反向排序会使其按以下顺序排列:to、from、body。我只是一次性使用它,当我使用 msg.as_string() 时,我得到了一些奇怪的结果,所以我只是使用字符串。这些值是一个值的列表,因此我访问第 0 个条目以使其成为一个字符串。
I like Alexander's answer but it is in Python 2! We now get
urlparse()
andparse_qs()
fromurllib.parse
. Also note that sorting the header in reverse puts it in the order: to, from, body.I am just using this as a one-off, when I used
msg.as_string()
I got some strange results though so I just went with the string. The values are lists of one value so I access the 0'th entry to make it a string.