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

 更新时间:2019年02月14日 15:17:14   作者:黑面狐  
今天小编就为大家分享一篇基于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历史天气采集的分析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 更新pip3与pyttsx3文字语音转换的实现方法

    更新pip3与pyttsx3文字语音转换的实现方法

    今天小编就为大家分享一篇更新pip3与pyttsx3文字语音转换的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python制作Windows系统服务

    Python制作Windows系统服务

    这篇文章主要为大家详细介绍了Python制作Windows系统服务的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • pandas中的Timestamp只保留日期不显示时间

    pandas中的Timestamp只保留日期不显示时间

    这篇文章主要介绍了pandas中的Timestamp只保留日期不显示时间,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 在Python的Django框架的视图中使用Session的方法

    在Python的Django框架的视图中使用Session的方法

    这篇文章主要介绍了在Python的Django框架的视图中使用Session的方法,包括相关的设置测试Cookies的方法,需要的朋友可以参考下
    2015-07-07
  • Python编程基础之函数和模块

    Python编程基础之函数和模块

    这篇文章主要为大家介绍了Python函数和模块,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • Python中typing模块的具体使用

    Python中typing模块的具体使用

    本文主要介绍了Python中typing模块的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • Python制作钉钉加密/解密工具

    Python制作钉钉加密/解密工具

    本文给大家介绍的是使用Python实现钉钉的加密解密工具的核心代码,非常的实用,对于大家学习Python加密解密非常有帮助,希望大家能够喜欢
    2016-12-12
  • python程序需要编译吗

    python程序需要编译吗

    在本篇文章里小编给大家整理了关于python程序编译相关的知识点内容,有兴趣的朋友们参考学习下。
    2020-06-06
  • Python 如何优雅的将数字转化为时间格式的方法

    Python 如何优雅的将数字转化为时间格式的方法

    这篇文章主要介绍了Python 如何优雅的将数字转化为时间格式的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Django添加sitemap的方法示例

    Django添加sitemap的方法示例

    这篇文章主要介绍了Django添加sitemap的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08

最新评论