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'import urllib
import re

def getHtml(url):
        page=urllib.urlopen(url)
        html=page.read()
        page.close()
        return html
def getWeather(html):
        reg='<a title=.*?>(.*?)</a>.*?<span>(.*?)</span>.*?<b>(.*?)</b>'
        weatherList=re.compile(reg).findall(html)
        return weatherList

weatherList=getWeather(getHtml('http://gd.weather.com.cn/index.shtml'))
for weather in weatherList:
        print (str(weather).decode('utf-8').encode('gb2312')),(' '), (str(weather).decode('utf-8').encode('gb2312')),(' '),(str(weather).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版本


   
页: [1]
查看完整版本: 'module' object has no attribute 'urlopen'