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

yipeiwu_com5年前Python基础

分析历史天气的趋势。

先采集

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设计】。

相关文章

Python笔记之facade模式

Python笔记之facade模式

facade模式,即门面模式,也称外观模式,这个模式的核心思想是使用facade对象为外部客户端提供一个统一的访问一组子系统的接口,即客户端不会直接与各个子系统交互,而是通过facade...

跟老齐学Python之字典,你还记得吗?

字典,这个东西你现在还用吗?随着网络的发展,用的人越来越少了。不少人习惯于在网上搜索,不仅有web版,乃至于已经有手机版的各种字典了。我曾经用过一本小小的《新华字典》。 《新华字典》是中...

python如何生成各种随机分布图

python如何生成各种随机分布图

在学习生活中,我们经常性的发现有很多事物背后都有某种规律,而且,这种规律可能符合某种随机分布,比如:正态分布、对数正态分布、beta分布等等。 所以,了解某种分布对一些事物有更加深入的理...

python实现二分查找算法

二分查找算法:简单的说,就是将一个数组先排序好,比如按照从小到大的顺序排列好,当给定一个数据,比如target,查找target在数组中的位置时,可以先找到数组中间的数array[mid...

使用python开发vim插件及心得分享

使用python开发vim插件及心得分享

vim有各种强大的插件,这不仅归功于其提供的用来编写插件的脚本语言vimL,还得益于它良好的接口实现,从而支持python等语言编写插件。当vim编译时带有+python特性时就能使用p...