之前在:《鲜活的数据:数据可视化指南》第2章 收集数据 Python3.3源码 中改写了原书的代码为python3.3版本,为了更好的学习和熟悉BeautifulSoup,又改编了一个程序,从中文网站http://lishi.tianqi.com/beijing/...上来下载相关天气信息,并保存为CSV格式。
1 # coding = utf-8 2 """ 3 从http://lishi.tianqi.com/beijing/下载部分北京天气记录保存到文件中。 4 利用BeautifulSoup模块 5 """ 6 __author__ = 'hillfree' 7 8 9 10 from urllib import request 11 from bs4 import BeautifulSoup 12 13 14 def downloadPages(): 15 # Iterate through year, months and day 16 for year in range(2011, 2014): 17 for month in range(1, 13): 18 19 # 目前数据只提供从2011年1月至2013年3月的数据 20 if year == 2013 and month == 4: 21 break 22 23 stamp = "{0}{1:02d}".format(year, month) 24 url = "http://lishi.tianqi.com/beijing/{0}.html".format(stamp,) 25 filename = "BeijingWeather{0}.html".format(stamp,) 26 27 # 方法1: 直接使用urlretrieve()。 推荐 28 request.urlretrieve(url, filename) 29 print(stamp + "OK!") 30 31 # 方法2: 分别打开链接和文件写入 32 # page = request.urlopen(url) 33 # print(url + " Open OK!") 34 # # 注意此处一定用‘wb’打开文件 35 # file = open(filename, 'wb') 36 # file.write(page.read()) 37 # file.close() 38 # print(filename + "Save OK!") 39 # page.close() 40 41 def parsePageToCSV(): 42 output = open("BeijingWeather.csv", 'w', encoding="utf-8") 43 for year in range(2011, 2014): 44 for month in range(1, 13): 45 46 # 目前数据只提供从2011年1月至2013年3月的数据 47 if year == 2013 and month == 4: 48 break 49 50 stamp = "{0}{1:02d}".format(year, month) 51 filename = "BeijingWeather{0}.html".format(stamp,) 52 53 file = open(filename, 'r') 54 soup = BeautifulSoup(file) 55 56 57 monthData = soup.find(name="div", attrs={"class":"tqtongji2"}); 58 if monthData == None: 59 break 60 uls = monthData.findAll("ul")[1:] # 滤去表头汉字 61 for ul in uls: 62 lis = ul.findAll("li") 63 # 依次取出日期、最高气温、最低气温、天气、风向、风力 64 item = "{0}, {1}, {2}, {3}, {4}, {5}".format(lis[0].string, lis[1].string, 65 lis[2].string, lis[3].string, lis[4].string, lis[5].string, ) 66 print(item) 67 output.write(item + "\n") 68 69 output.close() 70 71 72 if __name__ == "__main__": 73 downloadPages() 74 parsePageToCSV()