最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Eclipse+PyDev中运行带urllib的python程序出错:IOError: [Errno http error] no host given

Python crifan 4502浏览 0评论

【问题】

为了回答:

python解析网页源代码返回乱码问题

的问题,折腾:

【问题解答】python解析网页源代码返回乱码问题

期间,在PyDev中写了段代码:

# -*- coding: utf-8 -*- 
'''
Created on Oct 18, 2013

@author: CLi
'''
from urllib import urlopen

def getHtml(url):
    page = urlopen(url)
    html = page.read()
    page.close()
    return html

if __name__ == "__main__":
    url = "http:/www.sina.com.cn"
    html = getHtml(url)
    print html

结果运行出错:

Traceback (most recent call last):

  File "D:\DevRoot\eclipse\common_root\pythonParseSinaHtml\pythonParseSinaHtml.py", line 17, in <module>

    html = getHtml(url)

  File "D:\DevRoot\eclipse\common_root\pythonParseSinaHtml\pythonParseSinaHtml.py", line 10, in getHtml

    page = urlopen(url)

  File "D:\tmp\dev_install_root\Python27_x64\lib\urllib.py", line 86, in urlopen

    return opener.open(url)

  File "D:\tmp\dev_install_root\Python27_x64\lib\urllib.py", line 207, in open

    return getattr(self, name)(url)

  File "D:\tmp\dev_install_root\Python27_x64\lib\urllib.py", line 320, in open_http

    if not host: raise IOError, (‘http error’, ‘no host given’)

IOError: [Errno http error] no host given

如图:

IOError Errno http error no host given

【解决过程】

1.找了半天,没找到原因。

以为是PyDev的路径等问题呢。

2.参考:

Help for urllib error : ‘no host given’

去看看自己代码,

果然是不小心漏了个斜杠:

http:/www.sina.com.cn

应该是:

http://www.sina.com.cn

所以改为:

url = "http://www.sina.com.cn"

即可。

 

【总结】

不小心,害死人啊。。。

转载请注明:在路上 » 【已解决】Eclipse+PyDev中运行带urllib的python程序出错:IOError: [Errno http error] no host given

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.181 seconds, using 22.15MB memory