python爬虫天气

python爬虫天气,第1张

python本来就是一个多样化的语言,天气应用怎么能少呢?

库:

requests

re

random

time

urllib.request

xes_lib

自己pip 吧!

pip install 库名

上代码:


import re,random,time
import urllib.request

flag=1
def main(LZ):
    global flag
    if flag:
        def fun(line,flag=1):
            for i in range(len(line)):
                print("\r"+line[0:i+1],end="")
                time.sleep(0.1)
            if flag:
                print()

        flag=0


    print("\033[1;33m",end="")
    a=input("请输入查询省份的拼音:")
    b=input("请输入查询城市的拼音:")
    print("\033[1;36m",end="")
    def weather0(a,b):

        hearders = "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
        url = "https://tianqi.moji.com/weather/china/{}/{}".format(a,b)
        par = '()'
        opener = urllib.request.build_opener()
        opener.addheaders = [hearders]
        urllib.request.install_opener(opener)
        html = urllib.request.urlopen(url).read().decode("utf-8")
        data = re.search(par,html).group(2)
        print()
        if not data or not a or not b:
            print("抱歉,未查询到!信息在完善中······")
            return
        data = data.replace(",",",").replace("。",",").replace("墨迹天气建议您","WXT"+"提醒:")
        data = data.split(",")

        str1=data[3]+" "+data[4]
        str2=data[5]+" "+data[6]+"\n"
        data[3:5]=[str1]
        data[4:6]=[str2]
        for i in range(len(data)):
            data[i]=data[i].lstrip()
            if i==5:
                print(data[i],end=" ")
            elif "WXT"in data[i]:
                print("\n"+data[i],end=",")
            else:
                print(data[i])
    def weather1(a,b):

        hearders = "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
        url = "https://tianqi.moji.com/tommorrow/china/{}/{}".format(a,b)
        par = '()'
        opener = urllib.request.build_opener()
        opener.addheaders = [hearders]
        urllib.request.install_opener(opener)
        html = urllib.request.urlopen(url).read().decode("utf-8")
        data = re.search(par,html).group(2)
        print()
        if not data or not a or not b:
            print("抱歉,未查询到!信息在完善中······")
            return
        data = data.replace(",",",").replace("。",",").replace("墨迹天气建议您","WXT"+"提醒:")
        data = data.split(",")

        str1=data[3]+" "+data[4]

        data[3:5]=[str1]

        for i in range(len(data)):
            data[i]=data[i].lstrip()
            if i==5:
                print(data[i],end=" ")
            elif "WXT"in data[i]:
                print("\n"+data[i],end=",")
            else:
                print(data[i])
    def weather2(a,b):

        hearders = "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
        url = "https://tianqi.moji.com/tdat/china/{}/{}".format(a,b)
        par = '()'
        opener = urllib.request.build_opener()
        opener.addheaders = [hearders]
        urllib.request.install_opener(opener)
        html = urllib.request.urlopen(url).read().decode("utf-8")
        data = re.search(par,html).group(2)
        print()
        if not data or not a or not b:
            print("抱歉,未查询到!信息在完善中······")
            return
        data = data.replace(",",",").replace("。",",").replace("墨迹天气建议您","WXT"+"提醒:")
        data = data.split(",")

        str1=data[3]+" "+data[4]

        data[3:5]=[str1]

        for i in range(len(data)):
            data[i]=data[i].lstrip()
            if i==5:
                print(data[i],end=" ")
            elif "WXT"in data[i]:
                print("\n"+data[i],end=",")
            else:
                print(data[i])
    weather0(a,b)
    print("明天:")
    weather1(a,b)
    print("后天")
    weather2(a,b)
    print("\033[1;36m",end="")
    print("\n-------------------------")
    quit=input("继续查询请按1,其余键退出")
    print("\033[1000A\033[2J",end="")

    if quit == "1":
        print("欢迎下次使用,祝您生活愉快!")
        return
main("刘贞")

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/793911.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存