当前位置: 移动技术网 > IT编程>脚本编程>Python > 基于python历史天气采集的分析

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

2019年05月29日  | 移动技术网IT编程  | 我要评论

邢台都市网,爱非喜,官员威胁纪委

分析历史天气的趋势。

先采集

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/",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历史天气采集的分析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网