python3爬虫--抓取天气信息

    xiaoxiao2026-08-03  6

    抓取中国天气网上的数据并打出

    Version:Python3.5  Os:win10

    import urllib.request import re import time if __name__=='__main__': url = 'http://www.weather.com.cn/weather/101010100.shtml' header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} req=urllib.request.Request(url=url,headers=header) webPage=urllib.request.urlopen(req) data = webPage.read() data = data.decode('utf-8') date=time.strftime("%Y-%m-%d %H:%M %p", time.localtime()) regzone='<a href="http://bj.weather.com.cn" target="_blank">(.*?)</a>' regdis='<p class="wea" title="(.*?)">' reg='<p class=.*?>\n<span>(.*?)</span>/\n<i>(.*?)</i>' w=re.compile(reg).findall(data) wz=re.compile(regzone).findall(data) wdis=re.compile(regdis).findall(data) print(date,wz[0],w[0],wdis[0]) 然后写一个gui界面:

    from tkinter import * import urllib.request import re import time url = 'http://www.weather.com.cn/weather/101010100.shtml' header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} req=urllib.request.Request(url=url,headers=header) webPage=urllib.request.urlopen(req) data = webPage.read() data = data.decode('utf-8') date=time.strftime("%Y-%m-%d %H:%M %p", time.localtime()) regzone='<a href="http://bj.weather.com.cn" target="_blank">(.*?)</a>' regdis='<p class="wea" title="(.*?)">' reg='<p class=.*?>\n<span>(.*?)</span>/\n<i>(.*?)</i>' w=re.compile(reg).findall(data) wz=re.compile(regzone).findall(data) wdis=re.compile(regdis).findall(data) class Application(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createWidgets() def createWidgets(self): self.helloLabel = Label(self, text=date+' '+wz[0]+'\n'+wdis[0]+'\n'+'气温范围'+str(w[0])) self.helloLabel.pack() app = Application() # 设置窗口标题: app.master.title('实时温度') # 主消息循环: app.mainloop()

    转载请注明原文地址: https://ju.6miu.com/read-1310813.html
    最新回复(0)