栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python爬虫天气

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python爬虫天气

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("33[1;33m",end="")
    a=input("请输入查询省份的拼音:")
    b=input("请输入查询城市的拼音:")
    print("33[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("33[1;36m",end="")
    print("n-------------------------")
    quit=input("继续查询请按1,其余键退出")
    print("33[1000A33[2J",end="")

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857880.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号