Python urllib2 确保 url 格式良好
在 ValueError : urllib2 中的未知 url 类型,虽然在浏览器中打开该 url 是可以的,但指出在调用 opener.open()
之前必须确保传递给它的 url是格式良好(即 - HTTP url 具有“http://”前缀,FTP 具有“ftp://”前缀等)。
该问题被细化为“是否可以使用某些内置函数自动处理此类情况,或者我必须对后续的字符串连接进行错误处理?”或者换句话说:是否有内置的 Python 可以做到这一点?
然而,这个精致的问题从未得到解答,因此在这里重新提出。自己做起来很容易,但为什么要重新发明轮子呢?
In ValueError: unknown url type in urllib2, though the url is fine if opened in a browser, it was pointed out that before calling opener.open()
you must ensure that the url passed to it is well-formed (ie - has a "http://" prefix for HTTP urls, "ftp://" for FTP, etc).
The question was refined to ask "Is it possible to handle such cases automatically with some builtin function or I have to do error handling with subsequent string concatenation?" Or put another way: is there a Python built-in for doing this?
However, this refined question was never answered, hence the re-asking here. It's easy enough to do myself, but why reinvent the wheel right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Python 中,接受和异常而不是提前签入值是很常见的。所以这样的事情对我来说是完美的,可能对大多数 python 程序员来说也是如此:
但我不明白你想如何自动处理没有前缀的 url。前缀定义要使用的协议。如果没有给出,你如何“猜测”它?
In Python it's quite common to accept and exception instead of checkin the value in advance. So something like this would be perfectly of for me and probably for most python programmers:
But I don't see how you would like to handle urls without prefix automatically. The prefix defines the protocol to be used. If it's not given, how would you "guess" it?
如果您想默认添加
http://
,您确实需要自己执行此操作。没有理由说这应该比前置例如gopher:
或mailto:
或news:
更好 - 有很多协议。仅仅因为当今的网络浏览器向用户隐藏了
http://
前缀,并不意味着它就已经过时了。If you want to default to prepending
http://
, you really need to do this on your own. There is no reason why this should be better than prepending e.g.gopher:
ormailto:
ornews:
- there are plenty of protocols.Just because web-browsers today hide the
http://
prefix from their users doesn't make it obsolete.