分析历史天气的趋势。

先采集

基于python历史天气采集的分析

基于python历史天气采集的分析

基于python历史天气采集的分析

代码:

#-*- coding:utf-8 -*-
import requests
import random
import MySQLdb
import xlwt
from bs4 import BeautifulSoup
user_agent=['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36',
    'Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10',
    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER',
    'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)',
    ]
headers={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8',
'User-Agent': user_agent[random.randint(0,5)]}
 
myfile=xlwt.Workbook()
wtable=myfile.add_sheet(u"历史天气",cell_overwrite_ok=True)
wtable.write(0,0,u"日期")
wtable.write(0,1,u"最高温度")
wtable.write(0,2,u"最低温度")
wtable.write(0,3,u"天气")
wtable.write(0,4,u"风向")
wtable.write(0,5,u"风力")
 
db = MySQLdb.connect('localhost','root','liao1234','liao',charset='utf8')
cursor = db.cursor()
 
index = requests.get("http://lishi.tianqi.com/binjianqu/index.html",headers=headers)
html_index = index.text
index_soup = BeautifulSoup(html_index)
i = 1
for href in index_soup.find("div",class_="tqtongji1").find_all("a"):
  print href.attrs["href"]
 
 
  url = href.attrs["href"]
  r = requests.get(url,headers = headers)
  html = r.text
  #print html
  soup = BeautifulSoup(html)
  ss = []
  s = []
  for tag in soup.find("div",class_="tqtongji2").find_all("li"):
    print tag.string
    s.append(tag.string)
    if len(s) == 6:
      ss.append(s)
      s = []
  flag = 0
  for s in ss:
    if flag == 0:
      flag = 1
      continue
    else:
      sql = "insert into weather(old_date,hight,low,weather,wind,wind_power) values('%s','%s','%s','%s','%s','%s')"%(s[0],s[1],s[2],s[3],s[4],s[5])
      cursor.execute(sql)
      wtable.write(i,0,s[0])
      wtable.write(i,1,s[1])
      wtable.write(i,2,s[2])
      wtable.write(i,3,s[3])
      wtable.write(i,4,s[4])
      wtable.write(i,5,s[5])
      i += 1
myfile.save("weather.xls")
db.close()

以上这篇基于python历史天气采集的分析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,历史天气,采集

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“基于python历史天气采集的分析”
暂无“基于python历史天气采集的分析”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。