吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 10940|回复: 38
收起左侧

[Python 转载] 【Python爬虫原创代码】利用python爬取全国各地天气预报

[复制链接]
袅袅系秋风 发表于 2019-3-22 21:27
本帖最后由 袅袅系秋风 于 2019-3-23 18:20 编辑

最近在学习python爬虫,写了这个程序练手,有谁想参考的可以看一下,大家共同学习。



程序利用的是python的requests模块以及re模块


主要通过requests.get()获取网页代码保存到response中,然后通过正则从html代码中提取自己想要的数据。


最后将这些数据封装为图形化界面


程序运行截图如下:

1.png
(tip: 也可以通过字符查询,默认显示查询的第一个,如果想要显示准确的地方天气建议填完整城市名或完整城市拼音)




下面是代码:



[Python] 纯文本查看 复制代码
from tkinter import *
import tkinter as tk
import requests
from PIL import ImageTk as itk


class MyFrame(Frame):
    def __init__(self):
        self.root=Tk()

        self.root.title("天气查询")
        self.root.geometry('1200x700+400+220')

        bg = tk.Canvas(self.root, width=1200, height=600, bg='white')
        self.img = itk.PhotoImage(file="bg.gif")
        bg.place(x=100, y=40)
        bg.create_image(0, 0, anchor=NW, image=self.img)

        self.city = Entry(self.root, width=16, font=("仿宋", 18, "normal"))
        self.city.place(x=200, y=60)

        citylabel=Label(self.root,text='查询城市',font=("仿宋", 18, "normal"))
        citylabel.place(x=80,y=60)

        [i]#查询按钮
[/i][i]        [/i]chaxun = Button(self.root, width=10, height=3, text="查询", bg='#00CCFF', bd=5, font="bold")
        chaxun.bind("<Button-1>", self.search)
        chaxun.place(x=800, y=50)

        self.result=Listbox(self.root,heigh=18,width=65,font=("仿宋", 20, "normal"))[i]#显示天气框
[/i][i]        [/i]self.result.place(x=125,y=120)

    def tianqiforecast(self,searchcity):
        print('请输入所要查询天气的城市:')
        city = searchcity
        [i]# city='minquan'
[/i][i]        [/i]url = 'http://toy1.weather.com.cn/search?cityname=' + city + '&callback=success_jsonpCallback&_=1548048506469'
[i]# print(url)
[/i][i]        [/i]headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
            'Cookie': '__guid=182823328.3322839646442213000.1543932524694.901; vjuids=1858d43b6.167798cbdb7.0.8c4d7463d5c5d; vjlast=1543932526.1543932526.30; userNewsPort0=1; f_city=%E5%B9%B3%E9%A1%B6%E5%B1%B1%7C101180501%7C; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1543932526,1543932551,1543932579; Wa_lvt_1=1547464114,1547464115,1547880054,1547983123; defaultCty=101181001; defaultCtyName=%u5546%u4E18; monitor_count=6; Wa_lpvt_1=1547983809'
}
        response = requests.get(url, headers=headers)
        html1 = response.content.decode('utf-8')
        [i]# print(html)
[/i][i]
[/i][i]        [/i]citys = re.findall('"ref":"(.*?)~.*?~(.*?)~.*?~(.*?)~.*?~.*?~.*?~.*?~(.*?)"', html1, re.S)
        if (len(citys) == 0):
            print('未查找到该城市')
            exit(-5)
        for i in range(0, len(citys)):
            print(i + 1, ':%14s%14s%14s%14s ' % (citys[i][0], citys[i][3], citys[i][2], citys[i][1]))
        [i]#choose = int(input('请选择城市编号:[1~' + str(len(citys)) + ']\n'))
[/i][i]        [/i]choose=1
        if (len(citys[choose - 1][0]) == 9):
            if (citys[choose - 1][0][0] != '1' or citys[choose - 1][0][1] != '0' or citys[choose - 1][0][2] != '1'):
                print('暂时无法查询国外天气,程序已退出')
                exit(404)
            else:
                url2 = 'http://www.weather.com.cn/weathern/' + citys[choose - 1][0] + '.shtml'
responseweather = requests.get(url2, headers=headers)
            html2 = responseweather.content.decode('utf-8')

            weather = re.findall('<li class="date-.*?".*?".*?">(.*?)</.*?"date-i.*?">(.*?)<.*?', html2, re.S)
            weather.append(re.findall(
                '<p class="weather-in.*?" title="(.*?)".*?title="(.*?)".*?title="(.*?)".*?<p class="wind-i.*?">(.*?)</p>',
                html2, re.S))
            Hightempture = re.findall(
                '<script>var eventDay =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', html2,
                re.S)
            Lowtempture = re.findall(
                'var eventNight =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];',
                html2, re.S)
            [i]# print(Hightempture,Lowtempture)
[/i][i]            [/i]b='查询城市为:'+ str(citys[choose - 1][3])+'    '+str(citys[choose - 1][1])
            self.result.insert(END, b)
            for i in range(0, 8):
                [i]# print(weather[i])
[/i][i]                [/i]'''print("%4s%4s%10s\t\t\t%s℃ ~ %s℃\t\t\t%s%s%-s" % (
                    weather[i][0], weather[i][1], weather[8][i][0], Lowtempture[0][i], Hightempture[0][i],
                    weather[8][i][1],
                    weather[8][i][3], weather[8][i][2]))'''
a = weather[i][0] + '    ' + weather[i][1] + '    ' + weather[8][i][0] + '    ' + Lowtempture[0][i] + '℃  ~  ' + \
                    Hightempture[0][i] + '℃   ' + weather[8][i][1]  + weather[8][i][3]+ weather[8][i][2]
                self.result.insert(END, a)

        if (len(citys[choose - 1][0]) == 12):
            url2 = 'http://forecast.weather.com.cn/town/weathern/' + citys[choose - 1][0] + '.shtml'
responseweather = requests.get(url2, headers=headers)
            html2 = responseweather.content.decode('utf-8')

            weather = re.findall('<li class="date-.*?".*?"da.*?">(.*?)</.*?"date-i.*?">(.*?)<.*?', html2, re.S)

            html2 = re.sub('lt;', '<', html2)
            weather.append(re.findall(
                '<p class="weather-in.*?" title="(.*?)".*?title="(.*?)".*?title="(.*?)".*?<p class="wind-i.*?">\\r\\n(.*?)\\r\\n',
                html2, re.S))

            Hightempture = re.findall(
                'var eventDay = \["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', html2, re.S)

            Lowtempture = re.findall(
                'var eventNight = \["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];',
                html2, re.S)
            [i]# print(Hightempture,Lowtempture)
[/i][i]            [/i]b='查询城市为:'+str(citys[choose - 1][3])+'   '+str( citys[choose - 1][2])+ '    ' +str(citys[choose - 1][1])
            self.result.insert(0,b)
            [i]#print(weather[8][2])
[/i][i]            #print(weather)
[/i][i]            [/i]for i in range(0, 8):
                [i]# print(weather[i])
[/i][i]                [/i]'''print("%4s%4s%10s\t\t\t%s℃ ~ %s℃\t\t\t%s%s%-s" % (
                    weather[i][0], weather[i][1], weather[8][i][0], Lowtempture[0][i], Hightempture[0][i],
                    weather[8][i][1],
                    weather[8][i][3], weather[8][i][2]))'''
a = weather[i][0] + '    ' + weather[i][1] + '    ' + weather[8][i][0] + '    ' + Lowtempture[0][i] + '℃  ~  ' + \
                    Hightempture[0][i] + '℃   ' + weather[8][i][1]  + weather[8][i][3]+ weather[8][i][2]
                self.result.insert(END, a)
    def search(self,event):
        mycity=self.city.get()
        if(mycity!=''):
            self.result.delete(0,END)
            self.city.delete(0,END)
            self.tianqiforecast(mycity)

if __name__=='__main__':
    myframe=MyFrame()
    myframe.root.mainloop()


想下载的也可以在这里下载源代码

天气预报带ui界面.rar

2.12 KB, 下载次数: 192, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 4吾爱币 +4 热心值 +3 收起 理由
ArgentumPC + 1 热心回复!
虞珂啊 + 1 + 1 可以借鉴一下
huguo002 + 1 + 1 我很赞同!
niebaohua + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

niebaohua 发表于 2019-3-22 21:52
[Python] 纯文本查看 复制代码
from tkinter import *
import tkinter as tk
import requests
from PIL import ImageTk as itk


class MyFrame(Frame):
    def __init__(self):
        self.root=Tk()

        self.root.title("天气查询")
        self.root.geometry('1200x700+400+220')

        bg = tk.Canvas(self.root, width=1200, height=600, bg='white')
        self.img = itk.PhotoImage(file="bg.gif")
        bg.place(x=100, y=40)
        bg.create_image(0, 0, anchor=NW, image=self.img)

        self.city = Entry(self.root, width=16, font=("仿宋", 18, "normal"))
        self.city.place(x=200, y=60)

        citylabel=Label(self.root,text='查询城市',font=("仿宋", 18, "normal"))
        citylabel.place(x=80,y=60)

        #查询按钮
        chaxun = Button(self.root, width=10, height=3, text="查询", bg='#00CCFF', bd=5, font="bold")
        chaxun.bind("<Button-1>", self.search)
        chaxun.place(x=800, y=50)

        self.result=Listbox(self.root,heigh=18,width=65,font=("仿宋", 20, "normal"))#显示天气框
        self.result.place(x=125,y=120)

    def tianqiforecast(self,searchcity):
        print('请输入所要查询天气的城市:')
        city = searchcity
        # city='minquan'
        url = 'http://toy1.weather.com.cn/search?cityname=' + city + '&callback=success_jsonpCallback&_=1548048506469'
# print(url)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
            'Cookie': '__guid=182823328.3322839646442213000.1543932524694.901; vjuids=1858d43b6.167798cbdb7.0.8c4d7463d5c5d; vjlast=1543932526.1543932526.30; userNewsPort0=1; f_city=%E5%B9%B3%E9%A1%B6%E5%B1%B1%7C101180501%7C; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1543932526,1543932551,1543932579; Wa_lvt_1=1547464114,1547464115,1547880054,1547983123; defaultCty=101181001; defaultCtyName=%u5546%u4E18; monitor_count=6; Wa_lpvt_1=1547983809'
}
        response = requests.get(url, headers=headers)
        html1 = response.content.decode('utf-8')
        # print(html)

        citys = re.findall('"ref":"(.*?)~.*?~(.*?)~.*?~(.*?)~.*?~.*?~.*?~.*?~(.*?)"', html1, re.S)
        if (len(citys) == 0):
            print('未查找到该城市')
            exit(-5)
        for i in range(0, len(citys)):
            print(i + 1, ':%14s%14s%14s%14s ' % (citys[0], citys[3], citys[2], citys[1]))
        #choose = int(input('请选择城市编号:[1~' + str(len(citys)) + ']\n'))
        choose=1
        if (len(citys[choose - 1][0]) == 9):
            if (citys[choose - 1][0][0] != '1' or citys[choose - 1][0][1] != '0' or citys[choose - 1][0][2] != '1'):
                print('暂时无法查询国外天气,程序已退出')
                exit(404)
            else:
                url2 = 'http://www.weather.com.cn/weathern/' + citys[choose - 1][0] + '.shtml'
responseweather = requests.get(url2, headers=headers)
            html2 = responseweather.content.decode('utf-8')

            weather = re.findall('<li class="date-.*?".*?".*?">(.*?)</.*?"date-i.*?">(.*?)<.*?', html2, re.S)
            weather.append(re.findall(
                '<p class="weather-in.*?" title="(.*?)".*?title="(.*?)".*?title="(.*?)".*?<p class="wind-i.*?">(.*?)</p>',
                html2, re.S))
            Hightempture = re.findall(
                '<script>var eventDay =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', html2,
                re.S)
            Lowtempture = re.findall(
                'var eventNight =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];',
                html2, re.S)
            # print(Hightempture,Lowtempture)
            b='查询城市为:'+ str(citys[choose - 1][3])+'    '+str(citys[choose - 1][1])
            self.result.insert(END, b)
            for i in range(0, 8):
                # print(weather)
                '''print("%4s%4s%10s\t\t\t%s℃ ~ %s℃\t\t\t%s%s%-s" % (
                    weather[0], weather[1], weather[8][0], Lowtempture[0], Hightempture[0],
                    weather[8][1],
                    weather[8][3], weather[8][2]))'''
a = weather[0] + '    ' + weather[1] + '    ' + weather[8][0] + '    ' + Lowtempture[0] + '℃  ~  ' + \
                    Hightempture[0] + '℃   ' + weather[8][1]  + weather[8][3]+ weather[8][2]
                self.result.insert(END, a)

        if (len(citys[choose - 1][0]) == 12):
            url2 = 'http://forecast.weather.com.cn/town/weathern/' + citys[choose - 1][0] + '.shtml'
responseweather = requests.get(url2, headers=headers)
            html2 = responseweather.content.decode('utf-8')

            weather = re.findall('<li class="date-.*?".*?"da.*?">(.*?)</.*?"date-i.*?">(.*?)<.*?', html2, re.S)

            html2 = re.sub('lt;', '<', html2)
            weather.append(re.findall(
                '<p class="weather-in.*?" title="(.*?)".*?title="(.*?)".*?title="(.*?)".*?<p class="wind-i.*?">\\r\\n(.*?)\\r\\n',
                html2, re.S))

            Hightempture = re.findall(
                'var eventDay = \["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', html2, re.S)

            Lowtempture = re.findall(
                'var eventNight = \["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];',
                html2, re.S)
            # print(Hightempture,Lowtempture)
            b='查询城市为:'+str(citys[choose - 1][3])+'   '+str( citys[choose - 1][2])+ '    ' +str(citys[choose - 1][1])
            self.result.insert(0,b)
            #print(weather[8][2])
            #print(weather)
            for i in range(0, 8):
                # print(weather)
                '''print("%4s%4s%10s\t\t\t%s℃ ~ %s℃\t\t\t%s%s%-s" % (
                    weather[0], weather[1], weather[8][0], Lowtempture[0], Hightempture[0],
                    weather[8][1],
                    weather[8][3], weather[8][2]))'''
a = weather[0] + '    ' + weather[1] + '    ' + weather[8][0] + '    ' + Lowtempture[0] + '℃  ~  ' + \
                    Hightempture[0] + '℃   ' + weather[8][1]  + weather[8][3]+ weather[8][2]
                self.result.insert(END, a)
    def search(self,event):
        mycity=self.city.get()
        if(mycity!=''):
            self.result.delete(0,END)
            self.city.delete(0,END)
            self.tianqiforecast(mycity)

if __name__=='__main__':
    myframe=MyFrame()
    myframe.root.mainloop()
TABRIS1412 发表于 2019-3-22 21:50
niebaohua 发表于 2019-3-22 21:54
 楼主| 袅袅系秋风 发表于 2019-3-23 18:18
niebaohua 发表于 2019-3-22 21:52
[mw_shl_code=python,true]from tkinter import *
import tkinter as tk
import requests

请问下你是怎么发的这样格式的代码的
 楼主| 袅袅系秋风 发表于 2019-3-23 18:21
niebaohua 发表于 2019-3-22 21:52
[mw_shl_code=python,true]from tkinter import *
import tkinter as tk
import requests

不用了,我找到了
payload 发表于 2019-3-23 20:22
谢谢分享!
niebaohua 发表于 2019-3-24 12:10
袅袅系秋风 发表于 2019-3-23 18:18
请问下你是怎么发的这样格式的代码的

评论窗口 在表情 左边 ,,发帖的时候 也是一样的
fanxiaofan 发表于 2019-3-24 15:55
谢谢大佬  研究一下
 楼主| 袅袅系秋风 发表于 2019-3-24 17:39
justFunny 发表于 2019-3-24 13:14
GUI是用什么模块封装的啊?

我用的是tkinter模块
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-16 16:00

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表