Python urllib2 无法打开url

Python urllib2 unable to open 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

如有任何建议,我们将不胜感激! 谢谢 -V

Python 将 : 解释为 URL 中的端口号。您需要使用 quote

之类的东西强制它作为 url 拉入

参见未被接受的答案中的引用 here

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