Chinaunix

标题: 'module' object has no attribute 'urlopen' [打印本页]

作者: asdf2110    时间: 2013-06-08 09:22
标题: 'module' object has no attribute 'urlopen'
请教大家个问题,昨天网上找了个抓取网页天气的程序,为什么在 win32 cmd 终端执行老是报错呢,在linux就没事
win32:Python 3.3.2
linux:Python 2.4.3

win32 报错如下:

Traceback (most recent call last):
  File "gw.py", line 14, in <module>
    weatherList=getWeather(getHtml('http://gd.weather.com.cn/index.shtml'))
  File "gw.py", line 5, in getHtml
    page=urllib.urlopen(url)
AttributeError: 'module' object has no attribute 'urlopen'
  1. import urllib
  2. import re

  3. def getHtml(url):
  4.         page=urllib.urlopen(url)
  5.         html=page.read()
  6.         page.close()
  7.         return html
  8. def getWeather(html):
  9.         reg='<a title=.*?>(.*?)</a>.*?<span>(.*?)</span>.*?<b>(.*?)</b>'
  10.         weatherList=re.compile(reg).findall(html)
  11.         return weatherList

  12. weatherList=getWeather(getHtml('http://gd.weather.com.cn/index.shtml'))
  13. for weather in weatherList:
  14.         print (str(weather[0]).decode('utf-8').encode('gb2312')),(' '), (str(weather[1]).decode('utf-8').encode('gb2312')),(' '),(str(weather[2]).decode('utf-8').encode('gb2312'))
复制代码

作者: yongsheng_2012    时间: 2013-06-08 12:02
python版本的问题吧,你在windows下,dir(urllib),应该是没有urlopen的属性的

把你windows的python版本换为python 2.x 应该就可以了
作者: laike9m    时间: 2013-06-08 14:06
请用urllib.request
作者: asdf2110    时间: 2013-06-08 21:49
看来是版本问题, 用了 2.6 可以
回复 2# yongsheng_2012


   
作者: asdf2110    时间: 2013-06-08 21:50
这个我试过了, 也不对, 不知道咋回事
回复 3# laike9m


   
作者: lnwf5188588    时间: 2014-08-20 11:15
楼主的问题解决了木有,我也遇到了这个问题?
作者: HH106    时间: 2014-08-20 14:00
回复 6# lnwf5188588
上面不是回复了版本问题吗?用2.6或2.7版本


   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2