Python爬虫案例六:抓取某个地区某月份天气数据并保存到mysql数据库中

复制代码
测试链接:https://lishi.tianqi.com/guangzhou/202003.html

源码:

复制代码
import requests, pymysql
from lxml import etree
class ThSpider(object):
    def __init__(self):
        # 初始化
        self.month_list = ['202101', '202102', '202103', '202104', '202105', '202106', '202107', '202108', '202109', '202110', '202111', '202112', '202201', '202202']
        # 链接数据库
        self.cz = pymysql.connect(
            host='127.0.0.1',
            port=3306,
            user='root',
            password='root',
            db='基本操作',
            charset='utf8'
        )
        # 创建游标
        self.kit = self.cz.cursor()
        # 创建数据库
        table_sql = \
            '''
                create table if not exists 天气数据 (
                    日期 varchar (50),
                    最高气温 varchar (50),
                    最低气温 varchar (50),
                    天气 varchar (50),
                    风向 varchar (50)
                );
            '''
        self.kit.execute(table_sql)

    def request_start_url(self):
        # 发送请求 + 得到响应
        for month in self.month_list:
            start_url = 'https://lishi.tianqi.com/shanghai/{}.html'.format(month)
            self.headers = {
                'Cookie': 'UserId=17209281674394559; Hm_lvt_7c50c7060f1f743bccf8c150a646e90a=1720928176; HMACCOUNT=66A8254591DC78E3; Hm_lpvt_7c50c7060f1f743bccf8c150a646e90a=1720941400',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
            }
            response = requests.get(start_url, headers=self.headers).text
            self.parse_response(response)
        self.commit_close()  # 也可在此处调用提交函数  请求一个月份,解析一个月份,然后提交数据库

    def parse_response(self, response):
        # 解析响应 + 得到字段
        A = etree.HTML(response)
        li_list = A.xpath('//ul[@class="thrui"]/li')
        for li in li_list:
            # 日期
            rq = li.xpath('./div[1]/text()')[0]
            # 最高气温
            gw = li.xpath('./div[2]/text()')[0]
            # 最低气温
            dw = li.xpath('./div[3]/text()')[0]
            # 天气
            tq = li.xpath('./div[4]/text()')[0]
            # 风向
            fx = li.xpath('./div[5]/text()')[0]
            # print(rq, gw, dw, tq, fx)
            insert_sql = \
                '''
                    insert into 天气数据 values ("{}", "{}", "{}", "{}", "{}")
                '''.format(rq, gw, dw, tq, fx)
            self.kit.execute(insert_sql)
            print('ok --{}'.format(rq))

    def commit_close(self):
        # 数据库的提交和关闭
        self.kit.close()
        self.cz.commit()
        self.cz.close()

    def main(self):
        self.request_start_url()
        # self.commit_close()

if __name__ == '__main__':
    th = ThSpider()
    th.main()

运行效果:

相关推荐
一瓢西湖水12 小时前
列式数据库-以clickHouse为例
数据库·clickhouse
Elastic 中国社区官方博客12 小时前
使用 Elastic Cloud Serverless 扩展批量索引
大数据·运维·数据库·elasticsearch·搜索引擎·云原生·serverless
liulanba12 小时前
AI Agent技术完整指南 第一部分:基础理论
数据库·人工智能·oracle
逆天小北鼻12 小时前
Oracle 服务端与客户端的核心区分要点
数据库·oracle
2501_9462429312 小时前
MPV-EASY Player (MPV播放器) v0.41.0.1
数据库·经验分享·云计算·计算机外设·github·电脑·csdn开发云
秃了也弱了。12 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
Dfreedom.12 小时前
从 model(x) 到__call__:解密深度学习框架的设计基石
人工智能·pytorch·python·深度学习·call
weixin_4250230013 小时前
Spring Boot 配置文件优先级详解
spring boot·后端·python
MySQL实战13 小时前
Redis 7.0 新特性之maxmemory-clients:限制客户端内存总使用量
数据库·redis
VX:Fegn089513 小时前
计算机毕业设计|基于springboot + vue校园社团管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计