- 论坛徽章:
- 7
|
请教大家个问题,昨天网上找了个抓取网页天气的程序,为什么在 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[0]).decode('utf-8').encode('gb2312')),(' '), (str(weather[1]).decode('utf-8').encode('gb2312')),(' '),(str(weather[2]).decode('utf-8').encode('gb2312'))
复制代码 |
|