python爬虫代码能够正常运行,但是数据无法获取。求大神指导

rookieflying 2017-03-29 05:07:03
#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")
...全文
1128 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

7,394

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 数据仓库
社区管理员
  • 数据仓库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧