python 爬取历史天气


官网:http://lishi.tianqi.com/luozhuangqu/201802.html

# encoding:utf-8
import requests
from bs4 import BeautifulSoup
import pymysql
import pandas as pd date_list = [x.strftime('%Y%m') for x in list(pd.date_range(start='2016-09', end='2018-09', freq="m"))]
url_str = "http://lishi.tianqi.com/"
# citys = ["苍山", "费县", "河东区", "莒南", "临沭", "兰山市", "罗庄区", "蒙阴", "平邑", "郯城", "沂南", "沂水"]
# city_code = ["cangshan", "feixian", "hedong", "junan", "linshu", "lanshan", "luozhuangqu", "mengyin", "pingyi",
# "tancheng", "yinan", "yishui"] city_code = ["yishui"] urls = []
# url拼接
for city in city_code:
for date_item in date_list:
url = url_str + city + "/" + date_item + ".html"
urls.append(url) # 数据爬取
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
weather_list = soup.select('div[class="tqtongji2"]')
for weather in weather_list:
weather_date = weather.select('a')[0].string.encode('utf-8')
ul_list = weather.select('ul')
for ul in ul_list[1:]:
li_list = ul.select('li')
tCity = "沂水"
tDate = li_list[0].string
tTopTem = li_list[1].string
tLowTem = li_list[2].string
tWeather = li_list[3].string
tWindDir = li_list[4].string
tWindPower = li_list[5].string # 数据库存储
conn = pymysql.connect(host='localhost', user='root', passwd='', database='weather',
charset='utf8') # 链接数据库
cursor = conn.cursor() # 获得游标
# 向数据库添加数据的SQL语句
sql = "insert into mWeather (mCity,mDate,mTopTem,mLowTem,mWeather,mWindDir,mWindPower) values ('%s','%s','%s','%s','%s','%s','%s')" \
% (tCity, tDate, tTopTem, tLowTem, tWeather, tWindDir, tWindPower)
cursor.execute(sql) # 执行
conn.commit() # 提交添加数据的命令
cursor.close()
conn.close()
print(tCity + " 城市 " + tDate + " 数据 ----- 爬取成功!")


python 爬取历史天气的更多相关文章

  1. PHP爬取历史天气

    PHP爬取历史天气 PHP作为宇宙第一语言,爬虫也是非常方便,这里爬取的是从天气网获得中国城市历史天气统计结果. 程序架构 main.php <?php include_once(". ...

  2. Python爬取中国天气网

    Python爬取中国天气网 基于requests库制作的爬虫. 使用方法:打开终端输入 “python3 weather.py 北京(或你所在的城市)" 程序正常运行需要在同文件夹下加入一个 ...

  3. Scrapy实战篇(五)之爬取历史天气数据

    本篇文章我们以抓取历史天气数据为例,简单说明数据抓取的两种方式: 1.一般简单或者较小量的数据需求,我们以requests(selenum)+beautiful的方式抓取数据 2.当我们需要的数据量较 ...

  4. python爬取中国天气网站数据并对其进行数据可视化

    网址:http://www.weather.com.cn/textFC/hb.shtml 解析:BeautifulSoup4 爬取所有城市的最低天气   对爬取的数据进行可视化处理 按温度对城市进行排 ...

  5. PyQuery爬取历史天气信息

    1.准备工作: 网址:https://lishi.tianqi.com/xian/index.html 爬虫类库:PyQuery,requests 2.网页分析: 红线部分可更改为需要爬取的城市名,如 ...

  6. python爬虫实战(六)--------新浪微博(爬取微博帐号所发内容,不爬取历史内容)

    相关代码已经修改调试成功----2017-4-13 详情代码请移步我的github:https://github.com/pujinxiao/sina_spider 一.说明 1.目标网址:新浪微博 ...

  7. 初识python 之 爬虫:爬取中国天气网数据

    用到模块: 获取网页并解析:import requests,html5lib from bs4 import BeautifulSoup 使用pyecharts的Bar可视化工具"绘制图表& ...

  8. 毕设之Python爬取天气数据及可视化分析

    写在前面的一些P话:(https://jq.qq.com/?_wv=1027&k=RFkfeU8j) 天气预报我们每天都会关注,我们可以根据未来的天气增减衣物.安排出行,每天的气温.风速风向. ...

  9. python3爬取墨迹天气并发送给微信好友,附源码

    需求: 1. 爬取墨迹天气的信息,包括温湿度.风速.紫外线.限号情况,生活tips等信息 2. 输入需要查询的城市,自动爬取相应信息 3. 链接微信,发送给指定好友 思路比较清晰,主要分两块,一是爬虫 ...

随机推荐

  1. CentOS6.5如何设置NAT网络方式

    如何设置虚拟机与外网的连通. 今天说一下NAT的配置方法,其它两个都比较简单. 1.配置CentOS的IP [root@mycentos ~]# vi /etc/sysconfig/network-s ...

  2. express-generator 自动生成服务器基本文件

    (1) 安装 express-generator 构建工具 npm install -g express-generator 在命令行中用 npm 在全局安装 express-generator 脚手 ...

  3. iOS 图文混排

    使用系统自带的NSAttributedString来处理,对于一般的图文混排已经足够了,但是,有一个缺点就是NSAttributedString并不支持gif动画.实际上,使用gif动画还是挺卡的. ...

  4. python selenium截取指定元素图片

    1.截取当前屏幕 @property def getImage(self): ''' 截取图片,并保存在images文件夹 :return: 无 ''' timestrmap = time.strft ...

  5. python number

    一.number类型转换 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 complex(real ...

  6. C#6.0中10大新特性的应用和总结

    微软发布C#6.0.VS2015等系列产品也有一段时间了,但是网上的教程却不多,这里真对C#6.0给大家做了一些示例,分享给大家.   微软于2015年7月21日发布了Visual Studio 20 ...

  7. linux脚本文件执行的方法之间的区别

    sh/bash sh a.sh bash a.sh 都是打开一个subshell去读取.执行a.sh,而a.sh不需要有"执行权限",在subshell里运行的脚本里设置变量,不会 ...

  8. callback源码分析——callbacks

    uvm的callback必须是提供者有预见性的留一些方法在function前后,这样在使用的时候,进行遍历调度即可 设计者,需要从uvm_callback定义一个基类,只定义function原型,定义 ...

  9. 《A Structured Self-Attentive Sentence Embedding》(注意力机制)

    Background and Motivation: 现有的处理文本的常规流程第一步就是:Word embedding.也有一些 embedding 的方法是考虑了 phrase 和 sentence ...

  10. Vue系列之 => 钩子函数生命周期

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...