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

运行效果:

相关推荐
查无此人byebye5 小时前
【超详细解读(GPU)】基于DiT的MNIST扩散模型(DDPM)完整实现
python·深度学习·nlp·transformer·多分类
赵谨言5 小时前
基于Python和ArcPy的不动产数据入库技术与运用
大数据·开发语言·经验分享·python
—Miss. Z—5 小时前
计算机软件资格考试—Python补充
开发语言·python
nimadan125 小时前
**免费专业的小说创作软件2025推荐,解锁高效写作新体验*
人工智能·python
七夜zippoe5 小时前
PyTorch深度革命:从自动微分到企业级应用
人工智能·pytorch·python
好家伙VCC5 小时前
# 发散创新:基于ARCore的实时3D物体识别与交互开发实战 在增强现实(
java·python·3d·ar·交互
清水白石0085 小时前
函数签名内省实战:打造通用参数验证装饰器的完整指南
java·linux·数据库
知识分享小能手5 小时前
SQL Server 2019入门学习教程,从入门到精通,SQL Server 2019 事务和锁 — 语法知识点及使用方法详解(13)
数据库·学习·sqlserver
EXI-小洲5 小时前
2025年度总结 EXI-小洲:技术与生活两手抓
java·python·生活·年度总结·ai开发
之歆5 小时前
iSCSI + GFS2 + cLVM 共享存储完全指南
数据库