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读取数据集的图片(浅析Python 读取图像文件的性能对比)
- pythonselenium怎么设置元素(Python2 Selenium元素定位的实现8种)
- python3安装编程环境(Python3 修改默认环境的方法)
- pythonselenium接口自动测试(python3+selenium自动化测试框架详解)
- python处理水印(Yii 使用intervention/image拓展实现图像处理功能)
- python核心编程内容(顶级大神Linux,Python,Go,PHP之父谁是夜猫子?用Python揭秘!)
- python中if条件语句如何使用(对python中if语句的真假判断实例详解)
- python编程将一个三位数反序输出(python实现整数的二进制循环移位)
- python抽奖代码教程(python实现抽奖小程序)
- python批量转换图片格式(利用Python对文件夹下图片数据进行批量改名的代码实例)
- python统计图参数(Python使用统计函数绘制简单图形实例代码)
- python在txt指定行添加文本(Python修改文件往指定行插入内容的实例)
- python将网站图片本地存储(详解Python下载图片并保存本地的两种方式)
- python真的能高效处理excel报表吗(Python数据报表之Excel操作模块用法分析)
- python解析excel例子(Python玩转Excel的读写改实例)
- python中split使用方法(python lxml中etree的简单应用)
- 原创图画书,以儿童视角讲述中国故事(以儿童视角讲述中国故事)
- 八月再见 愿你岁月不扰,余生静好(八月再见愿你岁月不扰)
- 赏读 八月再见,九月你好(赏读八月再见九月你好)
- 散文 八月再见,九月,我在风中等你(散文八月再见九月)
- 8月再见 9月你好(8月再见)
- 魔兽世界 设计师爆料,原始版本并无PVP,跨阵营属于返璞归真(魔兽世界设计师爆料)
热门推荐
- python接口自动化接口依赖(python接口自动化十六--参数关联接口后传详解)
- python中什么是迭代器(一篇文章彻底搞懂Python中可迭代Iterable、迭代器Iterator与生成器Generator的概)
- mysql中innodb四种事务级别(MySQL InnoDB 锁的相关总结)
- javaweb购物车案例(js实现模拟购物商城案例)
- python对于gbk处理(Python 2/3下处理cjk编码的zip文件的方法)
- mysql中timestamp类型的CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP属性
- es的写入操作(详解ES9的新特性之异步遍历Async iteration)
- Web页面的字体有哪些
- js实现图片旋转
- 海外云服务器租用要怎么选择?(海外云服务器租用要怎么选择?)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9