python爬虫案例-爬取山东各城市近两年的天气情况(附带源码)

咱们先看爬取的数据

在这里插入图片描述

话不多说
直接开始代码的编写
首先导入第三方库
import requests
import json
import parsel # 解析数据 第三方模块
# 导入内置模块来保存文件
import csv
打开csv文件写入我们要爬取的数据表头
with open('山东各个城市历史天气.csv', mode='a', newline='', encoding='utf-8')as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(['日期','最高温','最低温','天气','风力风向','空气质量指数','城市'])

确定爬取的网站,然后拿到山东所有城市的ID
遍历爬取所有城市的天气
最后保存到本地
city_list=[54823,54734,54714,54736,54906,54915,54806,54938,54857,54945,54827,54843,54774,54765,54830,58024]
for city in city_list:

    for year in range(2021,2023):
        for month in range(1,13):
            url=f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D={city}&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'

            # 进行请求并接收
            response = requests.get(url = url)
            #返回<Response [200]> 说明接收正常
            #可以直接获取,不需要反爬措施

            # 获取数据,用json字典形式储存
            json_data = response.json()


            # 解析数据
            html_data= json_data['data']
            # .history-table tr
            selector=parsel.Selector(html_data)
            trs = selector.css('.history-table tr')[1:] # 不需要第一行
            for tr in trs:
                #::text:获取标签文本内容
                # .getall():  获取所有的td标签
                td = tr.css( 'td::text')
                if city==54823:
                    td.append('济南')
                elif city==54734:
                    td.append('滨州')
                elif city==54714:
                    td.append('德州')
                elif city==54736:
                    td.append('东营')
                elif city==54906:
                    td.append('菏泽')
                elif city==54915:
                    td.append('济宁')
                elif city==54806:
                    td.append('聊城')
                elif city==54938:
                    td.append('临沂')
                elif city==54857:
                    td.append('青岛')
                elif city==54945:
                    td.append('日照')
                elif city==54827:
                    td.append('泰安')
                elif city==54843:
                    td.append('潍坊')
                elif city==54774:
                    td.append('威海')
                elif city==54765:
                    td.append('烟台')
                elif city==54830:
                    td.append('淄博')
                elif city==58024:
                    td.append('枣庄')

                print(td)
                with open('山东各个城市历史天气.csv',mode='a',newline='',encoding='utf-8')as f:
                    csv_writer=csv.writer(f)
                    csv_writer.writerow(td)
全部源码放在这
# coding:utf-8
# author:songxin
# email:812067265@qq.com
# -- coding: utf-8 --
'''目标:爬取天气后报网山东各个城市天气'''
#导入requests模块
#在Terminal中输入pip install requests

import requests
import json
import parsel # 解析数据 第三方模块
# 导入内置模块来保存文件
import csv

with open('山东各个城市历史天气.csv', mode='a', newline='', encoding='utf-8')as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(['日期','最高温','最低温','天气','风力风向','空气质量指数','城市'])

# 确定目标网站 2345天气网
#先爬取山东济南历史天气
city_list=[54823,54734,54714,54736,54906,54915,54806,54938,54857,54945,54827,54843,54774,54765,54830,58024]
for city in city_list:

    for year in range(2021,2023):
        for month in range(1,13):
            url=f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D={city}&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'

            # 进行请求并接收
            response = requests.get(url = url)
            #返回<Response [200]> 说明接收正常
            #可以直接获取,不需要反爬措施

            # 获取数据,用json字典形式储存
            json_data = response.json()


            # 解析数据
            html_data= json_data['data']
            # .history-table tr
            selector=parsel.Selector(html_data)
            trs = selector.css('.history-table tr')[1:] # 不需要第一行
            for tr in trs:
                #::text:获取标签文本内容
                # .getall():  获取所有的td标签
                td = tr.css( 'td::text')
                if city==54823:
                    td.append('济南')
                elif city==54734:
                    td.append('滨州')
                elif city==54714:
                    td.append('德州')
                elif city==54736:
                    td.append('东营')
                elif city==54906:
                    td.append('菏泽')
                elif city==54915:
                    td.append('济宁')
                elif city==54806:
                    td.append('聊城')
                elif city==54938:
                    td.append('临沂')
                elif city==54857:
                    td.append('青岛')
                elif city==54945:
                    td.append('日照')
                elif city==54827:
                    td.append('泰安')
                elif city==54843:
                    td.append('潍坊')
                elif city==54774:
                    td.append('威海')
                elif city==54765:
                    td.append('烟台')
                elif city==54830:
                    td.append('淄博')
                elif city==58024:
                    td.append('枣庄')

                print(td)
                with open('山东各个城市历史天气.csv',mode='a',newline='',encoding='utf-8')as f:
                    csv_writer=csv.writer(f)
                    csv_writer.writerow(td)

  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
感谢您的提问。我理解您想了解如何用 Python 爬虫雪球网的金融数据。下面是一个简单的爬虫项目,可以帮助您实现这个目标: 1. 确定的目标网页 我们需要确定要的目标网页。在本例中,我们将使用雪球网的金融数据页面:https://xueqiu.com/hq。 2. 使用 requests 库发送 HTTP 请求 我们可以使用 Python 的 requests 库来发送 HTTP 请求并从目标网页获数据。以下是如何发送 GET 请求的示例代码: ```python import requests url = 'https://xueqiu.com/hq' response = requests.get(url) print(response.text) ``` 3. 解析 HTML 页面 我们可以使用 Python 的 Beautiful Soup 库来解析 HTML 页面。以下是如何使用 Beautiful Soup 库解析响应的 HTML 页面的示例代码: ```python from bs4 import BeautifulSoup soup = BeautifulSoup(response.text, 'html.parser') # 打印解析后的 HTML 页面 print(soup.prettify()) ``` 4. 提数据 一旦我们成功解析了 HTML 页面,就可以使用 Beautiful Soup 库中的一些方法来提我们需要的数据。以下是如何从 HTML 页面中提股票代码和股票名称的示例代码: ```python # 找到包含股票代码和名称的表格 table = soup.find('table', {'class': 'table'}) # 找到所有的行 rows = table.find_all('tr') # 遍历每一行,并提股票代码和名称 for row in rows: cols = row.find_all('td') if len(cols) > 0: code = cols[0].text.strip() name = cols[1].text.strip() print(code, name) ``` 5. 存储数据 最后,我们可以将提的数据存储到文件中。以下是如何将股票代码和名称存储到 CSV 文件中的示例代码: ```python import csv with open('stocks.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Code', 'Name']) for row in rows: cols = row.find_all('td') if len(cols) > 0: code = cols[0].text.strip() name = cols[1].text.strip() writer.writerow([code, name]) ``` 希望这个爬虫项目可以帮助您了解如何用 Python 爬虫雪球网的金融数据。如果您还有其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值