python - 如何从 url 中带有冒号 ":"的 url 获取 htmlcode?

标签 python urllib

我已经成功使用 python 和 urllib2 模块检索常规网页中的 html 代码。

但是当我尝试将它与带有冒号的网页一起使用时,它不起作用。 此代码:

f = urllib2.urlopen("http://http://gulasidorna.eniro.se/hitta:svenska+kyrkan/")
htmlcode = f.read()
print htmlcode

以下代码生成此错误消息。

File "/Users/jonathan/Documents/Dropbox/Python/eniro.py", line 137, in <module>
  f = urllib2.urlopen("http://http://gulasidorna.eniro.se/hitta:svenska+kyrkan/")
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 394, in open
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 412, in _open
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1199, in http_open
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1140, in do_open
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 693, in _init_
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 718, in _set_hostport
httplib.InvalidURL: nonnumeric port: ''

最佳答案

这应该可行,网址开头有一个额外的 http://:

f = urllib2.urlopen("http://gulasidorna.eniro.se/hitta:svenska+kyrkan/")
htmlcode = f.read()
print htmlcode

关于python - 如何从 url 中带有冒号 ":"的 url 获取 htmlcode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12704526/

相关文章:

python - 如何将 scipy.stats.describe 输出加载到 pandas 数据框中?

python - 读取基于 Web 的 JSON

python-3.x - 无法使用 xml.etree.ElementTree 解析 html

python - 使用 urllib、urllib2 和 request 避免类似 "Pyramid of Doom"的语法

python - Firebase Tensorflow Lite 分类模型未在 Swift 应用程序中提供正确的输出

python - 来自 WiFi 数据的位置

linux - 将 bash 脚本转换为 python

python -\ufeff 标识符中的无效字符

python - python 无法打开某些网站

.net - 我可以告诉 XmlTextWriter 编写 <element/> 而不是 <element/> 吗?