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()

运行效果:

相关推荐
拾心212 小时前
【云运维】Python基础(二)
python
Boilermaker19923 小时前
【MySQL 进阶】高性能优化
数据库·sql·mysql
CoderOnly3 小时前
SQL,CROSS JOIN速度优化
数据库·sql·mysql
Highcharts.js4 小时前
入门指南|从文件到图表:Highcharts对接数据库(CSV、Excel)实现数据同步绘制图表
数据库·excel·数据同步·highcharts·数据对接·文件导入
fish_study_csdn4 小时前
Python内存管理机制
开发语言·python·c python
老衲提灯找美女5 小时前
MySQL的增删改查功能合集
数据库·mysql·增删改查·增删改查详细用法
java1234_小锋6 小时前
[免费]基于Python的农产品可视化系统(Django+echarts)【论文+源码+SQL脚本】
python·信息可视化·django·echarts
Danceful_YJ6 小时前
31.注意力评分函数
pytorch·python·深度学习
程序员三藏6 小时前
快速弄懂POM设计模式
自动化测试·软件测试·python·selenium·测试工具·设计模式·职场和发展
SelectDB6 小时前
Apache Doris 4.0.1 版本正式发布
数据库·apache