Python urllib2 無法打開 url



我有一个这种格式的URL:

https://aLongStringWithNumbers:anotherLongStringWithNumbers@somewhere.com/admin/someAPICall.json

这看起来不是 Python 的 urllib2 可以理解的,在 urllib2.open 一起使用时不断出现错误:

raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])

这是库无法解释该 URL 的情况吗? 显然它可以在浏览器中工作...使用 Python 2.7

任何建议将不胜感激!谢谢- 五

Python 将:解释为 URL 中的端口号。您需要强制它作为 url 拉入

,例如 quote

在此处查看未接受答案的引用

>>> import urllib2
>>> urllib2.quote('https://aLongStringWithNumbers:anotherLongStringWithNumbers@somewhere.com/admin/someAPICall.json')

最新更新