2020-02-23

用python写一个简单的查看天气、新闻的程序

工具:pycharm、python3.8、火狐/谷歌浏览器
导入包:re、requests,sys
数据来源:天气—https://weather.com
新闻—凤凰网
上代码:

import re,requests
import sys


def main():
    while True:
        print('**************************')
        print('**********YTYG************')
        print('****天气---------1*********')
        print('****新闻---------2*********')
        print('****退出---------0*********')
        print('***************************')
        v = int(input('请选择对应的数字:  '))
        if v == 1:

            weath()
        elif v == 2:
            New()
        elif v == 0:
            sys.exit(0)
        else:
            print('请输入正确的数字  QAQ !!')

def New():
    def getnewsTitle():
        response = requests.get('http://www.ifeng.com/')
        response.encoding = 'utf-8'
        html = response.text
        reg = r'{"url":"(.*?)","title":"(.*?)","children":null}'
        newstitle = re.findall(reg, html)
        return newstitle

    for newstitle in getnewsTitle():
        newtitleUrl = newstitle[0]
        newTitle = newstitle[1]
        newTitles = newTitle.split('url')[0]
        print(newTitles)
        print(newtitleUrl)


def weath():
    def mainT():
        getcitylocation = input('请输入您所在城市: ')
        if getcitylocation == "常德":
            todaycd()
            print('查询完成,继续查询请按1,返回主页请按2 ')
            w = int(input('请选择对应的数字:  '))
            if w == 1:
                weath()
            elif w == 2:
                main()
        elif getcitylocation == "郑州":
            todayzz()
            print('查询完成,继续查询请按1,返回主页请按2 ')
            w = int(input('请选择对应的数字:  '))
            if w == 1:
                weath()
            elif w == 2:
                main()
        elif getcitylocation == '北京':
            todaybj()
            print('查询完成,继续查询请按1,返回主页请按2 ')
            w = int(input('请选择对应的数字:  '))
            if w == 1:
                weath()
            elif w == 2:
                main()

        else:
            print('目前天气服务不支持您所在城市')
            main()


    def todaycd():
        print('正在查询,请稍后')
        print(' ... ')

        def getCitycd():
            response = requests.get(
                'https://weather.com/zh-CN/weather/today/l/29.04,111.69?par=apple_widget&locale=zh_CN1')
            response.encoding = 'utf-8'
            html = response.text
            reg = r'<span class="today-wx-descrip" id="dp0-details-narrative">(.*?)</span>'
            felttemp = re.findall(reg, html)
            return felttemp

        for todaytempcd in getCitycd():
            print('常德今天天气: ' + todaytempcd)

    def todayzz():
        print('正在查询,请稍后')
        print(' ... ')

        def getCityzz():
            response = requests.get('https://weather.com/zh-CN/weather/today/l/CHXX0165:1:CH')
            response.encoding = 'utf-8'
            html = response.text
            reg = r'<span class="today-wx-descrip" id="dp0-details-narrative">(.*?)</span>'
            felttemp2 = re.findall(reg, html)
            return felttemp2

        for todaytempzz in getCityzz():
            print('郑州今天天气: ' + todaytempzz)

    def todaybj():
        print('正在查询,请稍后')
        print(' ... ')

        def getCitybj():
            response = requests.get('https://weather.com/zh-CN/weather/today/l/71ca347e2948ee9490525aa5433fa91da6973ae51ea0f765fbe8e85b9f16c5df')
            response.encoding = 'utf-8'
            html = response.text
            reg = r'<span class="today-wx-descrip" id="dp0-details-narrative">(.*?)</span>'
            felttemp3 = re.findall(reg,html)
            return felttemp3

        for todaytempbj in getCitybj():
            print('北京今天天气: ' + todaytempbj)
    mainT()
main()

本博客为原创,转载请标明来源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Gatsby Y

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

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

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

打赏作者

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

抵扣说明:

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

余额充值