基于python歷史天氣采集的分析
更新時(shí)間:2019年02月14日 15:17:14 作者:黑面狐
今天小編就為大家分享一篇基于python歷史天氣采集的分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
分析歷史天氣的趨勢(shì)。
先采集
代碼:
#-*- 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"風(fēng)向") wtable.write(0,5,u"風(fēng)力") 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歷史天氣采集的分析就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)解析yaml配置文件的示例詳解
在開(kāi)發(fā)過(guò)程中,配置文件是少不了的,而且配置文件是有專門(mén)的格式的,比如:ini,yaml,toml等等。本文帶大家來(lái)看看Python如何解析yaml文件,它的表達(dá)能力相比?ini?更加的強(qiáng)大,需要的可以參考一下2022-09-09詳細(xì)解析Python中__init__()方法的高級(jí)應(yīng)用
這篇文章主要介紹了詳細(xì)解析Python中__init__()方法的高級(jí)應(yīng)用,包括在映射和elif序列等地方的更為復(fù)雜的用法,需要的朋友可以參考下2015-05-05python 實(shí)現(xiàn)return返回多個(gè)值
今天小編就為大家分享一篇python 實(shí)現(xiàn)return返回多個(gè)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Python?numpy之線性代數(shù)與隨機(jī)漫步
這篇文章主要介紹了Python?numpy之線性代數(shù)與隨機(jī)漫步,線性代數(shù),矩陣計(jì)算,優(yōu)化與內(nèi)存;比如矩陣乘法,分解,行列式等數(shù)學(xué)知識(shí),是所有數(shù)組類庫(kù)的重要組成部分2022-07-07基于python進(jìn)行桶排序與基數(shù)排序的總結(jié)
今天小編就為大家分享一篇基于python進(jìn)行桶排序與基數(shù)排序的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05