igs网站里爬o文件和广播星历和tkinter使用
生活随笔
收集整理的這篇文章主要介紹了
igs网站里爬o文件和广播星历和tkinter使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 基礎要求
- 題目要求
- 代碼如下
- 結果如下
基礎要求
莫煩python–基礎
python基礎學習-個人博客
Tkinter 學習
題目要求
需求就是我要從這個網站里爬o文件和廣播星歷
武漢大學IGS數據中心
代碼如下
import time import urllib from tkinter import * import requests from bs4 import BeautifulSoup import os import random from tkinter.ttk import *# 假設是 headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", }# 文件存儲位置 dir = "E:\data"s = requests.Session()# 開始日期# igs觀測站列表 select = []# 被選擇的廣播站 choose = ""def download(download_list, name):'''下載文件到指定位置:param name: 文件夾名稱:param download_list::return:'''path = os.path.join(dir, name)# 如果下載路徑不存在,則創建文件夾if not os.path.exists(path):# 創建文件夾os.makedirs(path)txt.insert(INSERT, "dir文件路徑不存在,正在創建中\n")txt.insert(END, name + "下載開始,請稍等\n")for url in download_list:try:filename = os.path.join(path, get_randon_string() + get_filename(url))print("下載了--" + filename + "\n")urllib.request.urlretrieve(url, filename=filename)except Exception as e:txt.insert(END, "對不起下載失敗--可能存在相同名字的文件名,或者網絡問題")print(e)txt.insert(END, name + "下載結束\n")def get_randon_string():'''隨機生成數字類型字符串:return: 隨機字符串'''return str(random.randint(1, 50))def get_filename(url_str):'''根據url--獲取文件名稱:param url_str: url:return:'''try:return url_str[url_str.rfind('/') + 1:]except:print("文件名稱尋找失敗")return str(random.randint(1, 700)) + ".gz"def get_obs(choose, start_date, end_date):'''獲取obs觀測值:param choose: 選擇:param start_date: 開始時間:param end_date: 截止時間:return:'''if choose.strip() == '':txt.insert(END, "選擇的廣播站錯誤\n")if not is_valid_date(start_date):txt.insert(END, "起始時間輸入錯誤\n")if not is_valid_date(end_date):txt.insert(END, "起始時間輸入錯誤\n")url = "http://www.igs.gnsswhu.cn/index.php/home/data_product/get_obs_data.html?src=IGS&start_date=" + start_date + "&end_date=" + end_date + "&sites%5B%5D=" + choose + "&order=site&format=o"data = s.get(url, headers=headers)if data.text.strip() == '':txt.insert(END, "廣播星歷沒有任何數據\n")returnsubject = BeautifulSoup(data.text, "html.parser")download_list = []for item in subject.select(".site-item"):# 添加所有符合條件的下載地址temp = item.select("a")[0]["href"]download_list.append("http://wz-igs.oss-cn-beijing.aliyuncs.com/" + temp[temp.find('=') + 1:])# 開始下載download(download_list, "obs")def get_broadcast_ephemeris(start_date, end_date):'''獲取廣播新歷:param start_date: 開始時間:param end_date: 截止時間:return:'''if not is_valid_date(start_date):txt.insert(END, "起始時間輸入錯誤\n")if not is_valid_date(end_date):txt.insert(END, "起始時間輸入錯誤\n")URL = "http://www.igs.gnsswhu.cn/index.php/home/data_product/get_brdc_data.html?src=IGS&start_date=" + start_date + "&end_date=" + end_datedata = s.get(URL, headers=headers)if data.text.strip() == '':print("廣播星歷沒有任何數據")returnsubject = BeautifulSoup(data.text, "html.parser")# 下載鏈接download_list = []for item in subject.select(".am-success"):# 添加所有符合條件的下載地址temp = item.select("a")[0]["href"]download_list.append("http://wz-igs.oss-cn-beijing.aliyuncs.com/" + temp[temp.find('=') + 1:])# 開始下載文件download(download_list, "broadcast")def is_valid_date(times):'''判斷日期格式是否正確:param times: 字符串的日期:return:'''try:time.strptime(times, "%Y-%m-%d")except:txt.insert(END, "輸入日期的格式不合法哦,請重新檢查\n")return Falsereturn Truedef calc(event):'''點擊組合框的按鈕:param event: 時間信息:return:'''global choosechoose = select[comb.current()]def init():'''初始化:return:'''url = "http://www.igs.gnsswhu.cn/index.php/home/data_product/igs.html"data = s.get(url, headers=headers)subject = BeautifulSoup(data.text, "html.parser")for item in subject.select("#sites-selector option"):select.append(item.text)start = "2021-10-01" end = "2021-10-26"init() root = Tk() root.title('下載程序的窗口') root.geometry('500x400') # 這里的乘號不是 * ,而是小寫英文字母 x lb1 = Label(root, text='請輸入日期正確的日期') lb1.place(relx=0.5, rely=0.1, relwidth=0.5, relheight=0.1)# 第一個輸入框--起始時間 inp1 = Entry(root) inp1.place(relx=0.5, rely=0.2, relwidth=0.2, relheight=0.1)# 第二個輸入框---截止時間 inp2 = Entry(root) inp2.place(relx=0.8, rely=0.2, relwidth=0.2, relheight=0.1)btn1 = Button(root, text='下載.o 文件', command=lambda: get_obs(choose, inp1.get(), inp2.get())) btn1.place(relx=0.5, rely=0.4, relwidth=0.2, relheight=0.1)# 方法二利用 lambda 傳參數調用run2() btn2 = Button(root, text='下載廣播星歷', command=lambda: get_broadcast_ephemeris(inp1.get(), inp2.get())) btn2.place(relx=0.8, rely=0.4, relwidth=0.2, relheight=0.1)# 復選框 comb = Combobox(root, textvariable="選擇測試站", values=select) comb.place(relx=0.1, rely=0.2, relwidth=0.2, relheight=0.1) comb.bind('<<ComboboxSelected>>', calc)# 在窗體垂直自上而下位置60%處起,布局相對窗體高度40%高的文本框# scroll = Scrollbar(root) # scroll.place(relx=0.1, rely=0.6, relwidth=0.8, relheight=0.4) txt = Text(root) txt.place(relx=0.1, rely=0.6, relwidth=0.8, relheight=0.4) # scroll.config(command=txt.yview) # txt.config(yscrollcommand=scroll.set) # txt.pack()txt.insert(END, "程序開始\n") root.mainloop()結果如下
總結
以上是生活随笔為你收集整理的igs网站里爬o文件和广播星历和tkinter使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AI和IoT的新时代:告别独自美好,迎接
- 下一篇: volatile c 关键字