python气温变化数据分析(基于python历史天气采集的分析)
类别:脚本大全 浏览量:1867
时间:2022-03-28 12:33:06 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( "li" , 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( "li" , 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历史天气采集的分析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/qq1124794084/article/details/54174340
您可能感兴趣
- python编写程序读写数据库(详解js文件通过python访问数据库方法)
- python小程序编程代码(python实现烟花小程序)
- python多线程多进程运行场景(Python多线程同步---文件读写控制方法)
- python用pyqt5制作登录界面(python3+pyqt5+itchat微信定时发送消息的方法)
- python指定参数教程(详解Python传入参数的几种方法)
- python列表怎么赋值(详解Python列表赋值复制深拷贝及5种浅拷贝)
- python字符串的拼接与分割(Python字符串通过'+'和join函数拼接新字符串的性能测试比较)
- python数据结构全攻略(详解python数据结构和算法)
- python 读文件报错处理(解决python写入带有中文的字符到文件错误的问题)
- python常用的字符串操作方法(Python字符串的常见操作实例小结)
- python中列表操作五种常用方法(Python使用paramiko操作linux的方法讲解)
- python中if判断数据类型(基于python if 判断选择结构的实例详解)
- 2021-10-23 10:04:24
- python语言的读取图像(详解python读取image)
- python写一个二叉树(Python二叉搜索树与双向链表转换算法示例)
- python做了一个自动翻译的小工具(Python 20行简单实现有道在线翻译的详解)
- 红色代表什么(红色代表什么寓意)
- 蓝天代表什么(蓝天代表什么生肖)
- 今天要吃什么(今天要吃什么菜)
- 营养餐是什么(学校营养餐是什么)
- 谁说女子不如男 范冰冰演的武则天只是其一,另外两位你认识吗(谁说女子不如男)
- 杯酒人生---瓦伦丁酒杯和奥丁格啤酒(杯酒人生---瓦伦丁酒杯和奥丁格啤酒)
热门推荐
- jQuery中height()、innerheight()、outerheight()的区别
- sqlserver配置管理器不见了(SQL Server配置管理器无法连接到WMI提供程序)
- .NET中First,FirstOrDefault,Single,SingleOrDefault的区别
- docker集群安装教程(使用docker部署hadoop集群的详细教程)
- 如何加快页面的访问速度
- python3语法规则(详解Python3注释知识点)
- 用python如何写tkinter(浅谈python3.6的tkinter运行问题)
- pythonpandas数据类型(Python3.5 Pandas模块之Series用法实例分析)
- react native常用组件(react native环境安装流程)
- jquery on绑定hover
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9