获取城市天气预报,免费!

以下代码也是参考自网友分享,已经在本地测试通过。无页面显示,如果想用的话欢迎。有问题欢迎与我联系。
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup

def get_weather_info(url, choose_all=True):
    try:
        response = requests.get(url, timeout=2)
        # 发起请求并设置超时时间为两秒
        soup = BeautifulSoup(response.text, 'html.parser')
        # 解析HTML
        # 获取.day div的文本内容
        day_divs = soup.find_all('div', class_='day')
        day_text_list = [div.get_text() for div in day_divs]
        # 输出今天的城市和天气信息
        if choose_all:
            result = [",".join([item.strip() for item in day_text.split("\n") if item.strip()]) for day_text in day_text_list]
            #print(result)
            #print("result")
            return result
        else:
            for day_text in day_text_list:
                day_info = [item.strip() for item in day_text.split("\n") if item.strip()]
            print("day_info")
            #print(day_info)
            return day_info
    except requests.exceptions.RequestException as e:
        print("Error occurred:", e)
        return None

# 城市名和链接可以在https://weather.cma.cn/中添加更多城市,名字和链接对应的

href_list = ['58367', '51463', '52889', '54511', '58238', '58606', '58321', '53463', '50953', '53772', '59287', '56294',
             '55591', '56778', '58457', '57494', '54342', '54823', '59758', '59493', '53698', '58847', '52866', '57036',
             '57816', '57083', '57516', '53614', '54161', '57679', '54517', '45005', '45011', '58968', '59431',
             '062721', '003772', '068816', '061641', 'G05021', '065563', '063260', '064950', 'G05020', '063980',
             '061832', '065472', '067665', '067341', '064500', 'G05019', '061442', 'G05025', '063125', '063450',
             '063740', '065125', '064700', 'G05024', '067775', '060390', 'G05018', '063894', '064390', '008589',
             '063705', 'G05017', '065387', '064387', '060135', '067083', '060715', '040103', '062010', '062366',
             '064810', 'G05009', '061291', '047108', '044292', '048698', '097390', '048647', '048074', '048991',
             '048820', '048940', '096315', '041571', '043466', '041923', '044454', '043555', '041217', '040582',
             '017130', '041170', '041256', '040100', '040438', '041150', '040754', '040650', '040948', '037864',
             '037545', '037788', '035188', '038353', '038836', '038457', '048455', '096741', '098425', '041404',
             'G05010', '027612', '011035', '016716', '012375', '013274', '011520', '015614', '011816', '013615',
             '014240', '014654', '013462', '026038', '026730', '014014', '012843', '013586', '015420', '026422',
             '033345', '026850', '033815', 'G05005', '008535', 'G05011', '093439', '094035', 'G05007', 'G05022',
             'G05004', 'G05014', 'G05013', 'G05003', 'G05012', '085574', 'G05023', '006240', '086580', '080415',
             'G05016', 'G84071', '084628', '078762', '078806', 'G05015', 'G05006', '078970', '078862', 'G05026',
             'G05008', 'G05002', '078224', 'G05001', '047662', '072405', '006180', '094926', '076680', '040080',
             '001492', '040270', '061052', '083377', '007149', '087582', '006451', '047058', '042299', '076405',
             '002464', '042182', '010385', '065344', '071628', '064650', '065503', '016242', '040183', '067001',
             '002974', '061990', '064210', '038880', '008221', 'map', '58367', '51463', '52889', '54511', '58238',
             '58606', '58321', '53463', '50953', '53772', '59287', '56294', '55591', '56778', '58457', '57494', '54342',
             '54823', '59758', '59493', '53698', '58847', '52866', '57036', '57816', '57083', '57516', '53614', '54161',
             '57679', '54517', '45005', '45011', '58968', '59431', '54511', &
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用PyQt获取城市天气预报,可以通过以下步骤实现: 1. 首先,需要获取天气数据。可以使用爬虫方式获取天气网站的HTML页面,然后使用解析库(如BeautifulSoup)解析内容。另一种方法是使用天气预报网站提供的API接口直接获取结构化的数据,省去了解析HTML的工作。在这个例子中,我们选择使用中国天气网站提供的API接口\[2\]。 2. 使用PyQt创建一个界面,用于显示天气预报信息。可以使用PyQt的各种控件来设计界面,如标签、文本框等。在这个例子中,我们可以创建一个标签用于显示城市名,一个标签用于显示温度,一个标签用于显示风向等。 3. 在PyQt的主窗口类中,编写代码来获取天气数据并将其显示在界面上。可以使用requests库发送HTTP请求来获取天气数据\[3\]。然后,从返回的JSON数据中提取所需的天气信息,并将其设置到相应的标签中。 4. 最后,将界面文件转为.py文件,并调用主窗口类来显示界面。 通过以上步骤,我们可以实现一个使用PyQt获取城市天气预报的程序。 #### 引用[.reference_title] - *1* *3* [PyQt5快速开发与实战 10.1 获取城市天气预报](https://blog.csdn.net/weixin_44226181/article/details/126083561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [PyQt 5实战之城市天气预报](https://blog.csdn.net/qq_32019341/article/details/109564001)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_38111284

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值