7,394
社区成员
发帖
与我相关
我的任务
分享#coding=gbk
"""
Created on Mon Mar 06 15:28:51 2017
@author: Wei Qingdongsys.setdefaultencoding("u
"""
from __future__ import print_function
import urllib2
import codecs
from bs4 import BeautifulSoup
strYear = "2013"
strFile = "qingdaoWeather" + strYear + ".csv"
f = codecs.open(strFile, "w","gbk")
for month in range(1,13):
if(month < 10):
strMonth = '0' + str(month) + '-'
else:
strMonth = str(month) + '-'
strYearMonth = strMonth + strYear
print("\nGetting data for month" + strYearMonth + "...", end='')
url = "http://en.tutiempo.net/climate/"+strYearMonth+ "ws-548570" + ".html"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page)
weatherSet = soup.find(attrs={"class":"tooltip"})
if(weatherSet == None):
print("fail to get the page", end='')
continue
for line in weatherSet.contents:
if(line.__class__.__name__ == 'NavigableString'): continue
if(len(line.attrs) > 0): continue
lis = line.findAll('td') 

T = lis[0].text
TM = lis[1].text
Tm = lis[2].text
SLP = lis[3].text
H = lis[4].text
PP = lis[5].text
f.write(T +',' + TM + ',' + Tm +','+ SLP +','+H+','+ PP +'\n')
print("done", end='')
f.close()
print ("\nover")