美文网首页GIS
python爬取历史天气数据

python爬取历史天气数据

作者: 乐天c | 来源:发表于2017-06-03 09:46 被阅读0次

原文链接:

环境说明

扒取的网站:天气网,http://lishi.tianqi.com/

Python版本:2.7

操作系统:windows 7

所依赖的包(如若没有请安装)

包名说明官方地址

bs4是一个可以从HTML或XML文件中提取数据的Python库https://pypi.python.org/pypi/requests/

requests基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库https://pypi.python.org/pypi/requests/

xlwt本文中将获取得到的天气数据存储在xls中,需要对excel进行处理的包https://pypi.python.org/pypi/xlwt

# -*- coding=utf-8 -*-

from bs4 import BeautifulSoup

import requests import xlwt import os#获得某一个月的天气数据

defgetListByUrl(url):

res = requests.get(url)

soup = BeautifulSoup(res.text,"html.parser")

weathers = soup.select("#tool_site")

title = weathers[1].select("h3")[0].text

weatherInfors = weathers[1].select("ul")

weatherList = list()forweatherInforinweatherInfors:

singleWeather = list()forliinweatherInfor.select('li'):

singleWeather.append(li.text)

weatherList.append(singleWeather)

print(title)returnweatherList,title#@par:addressUrl 获得某地区的数据#@par:excelSavePath  数据的保存地址defgetListByAddress(addressUrl,excelSavePath):# url = "http://lishi.tianqi.com/beijing/index.html"url = addressUrl

res = requests.get(url)

soup = BeautifulSoup(res.text,"html.parser")

dates = soup.select(".tqtongji1 ul li a")

workbook = xlwt.Workbook(encoding='utf-8')fordindates:

weatherList,title = getListByUrl(d["href"])

booksheet = workbook.add_sheet(title,cell_overwrite_ok=True)fori,rowinenumerate(weatherList):forj,colinenumerate(row):

booksheet.write(i,j,col)

workbook.save(excelSavePath)if__name__ =="__main__":

addressName = raw_input("请输入即将获取天气的城市:\n")

addresses = BeautifulSoup(requests.get('http://lishi.tianqi.com/').text,"html.parser")

queryAddress = addresses.find_all('a',text=addressName)iflen(queryAddress):

savePath = raw_input("检测到有该城市数据,请输入即将保存天气数据的路径(如若不输入,将默认保存到c:/weather/"+addressName+".xls):\n")ifnotsavePath.strip():ifnotos.path.exists('c:/weather'):

os.makedirs('c:/weather')

savePath ="c:/weather/"+addressName+".xls"forqinqueryAddress:

getListByAddress(q["href"],savePath)

print("已经天气数据保存到:"+savePath)else:

print("不存在该城市的数据")

本代码是在windows下编辑的,如若想要在Linux下运行,请在头部加上#! python环境


本代码功能描述:

输入:

城市名称,如果没有则提示“不存在该城市的数据”

保存路径,如果不输入则默认保存在“c:/weather/城市名称.xls”

输出:

带有该城市的天气数据

代码演示

本程序是在pycharm IDE下开发的,直接利用pycharm执行,大家可以用别的编辑器来执行(注意,由于编码问题,如果直接用dos来执行会报错)

将getWeatherByQuery.py文件导入到pycharm下执行,如下图所示:

按照提示,输入城市,检测到有该城市,请求输入保存路径

在这里我们输入要保存的路径(当然也可以直接回车),在这里我输入“d:\weather\bj.xls”(请确保该目录存在,因为程序不会自动去创建目录)

此时程序会将各月份的数据导出

执行完之后结果如下图:

excel中的数据如下图:

相关文章

  • python爬取历史天气数据

    原文链接: 环境说明 扒取的网站:天气网,http://lishi.tianqi.com/ Python版本:2....

  • Python学习

    python爬虫(五) python爬虫爬取豆瓣电影Top250数据 利用python爬取豆瓣电影TOP250页面...

  • Python图形用户界面(GUI)的实现之tkinter

    使用工具:pycharm使用模块:tkinter 前言:前几期我们学会了Python爬取真气网天气数据Python...

  • Python学习

    python爬虫(四) python爬虫爬取豆瓣电影数据 今天我们爬取一下豆瓣电影TOP250页面电影的数据,依然...

  • Python爬取天气网数据并打包成exe

    参考文章:使用python爬取全国天气数据并导入MySQL数据库表[https://blog.csdn.net/q...

  • 【二】PYTHON爬取全国新房房价与浅析

    【一】学PYTHON及爬虫的一些总结 【二】PYTHON爬取全国新房房价与浅析 PART ONE:【数据采集】爬取...

  • Python爬虫:如何爬取分页数据?

    上一篇文章《产品经理学Python:如何爬取单页数据?》中说了爬取单页数据的方法,这篇文章详细解释如何爬取多页数据...

  • python 爬虫框架scrapy

    由于公司最近要写一个数据爬取工具,以前没接触过python 使用原生python 开发了一套携程国内酒店数据爬取,...

  • 利用python爬取股票交易数据

    利用python爬取股票交易数据 分析网站 找到返回的url 爬取股票交易数据 思路,我们利用request来请求...

  • Scrapy

    Scrapy简介和历史 Scrapy是用纯Python实现一个为了爬取网站数据、提取结构性数据而编写的应用框架,用...

网友评论

    本文标题:python爬取历史天气数据

    本文链接:https://www.haomeiwen.com/subject/drmpfxtx.html