python 爬取历史天气

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='123456', 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 + " 数据  -----   爬取成功!")
复制代码

 

 

 


 

posted @   叫我+V  阅读(376)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示