【问题】
为了回答:
的问题,折腾:
期间,在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): 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 |
如图:
【解决过程】
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