igs⽹站⾥爬o⽂件和⼴播星历和tkinter使⽤
⽂章⽬录
基础要求
题⽬要求
需求就是我要从这个⽹站⾥爬o⽂件和⼴播星历
代码如下
import time
import urllib
from tkinter import*
import requests
from bs4 import BeautifulSoup
import os
import random
k 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 ists(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")
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):
if not is_valid_date(start_date):
txt.insert(END,"起始时间输⼊错误\n")
if not is_valid_date(end_date):
txt.insert(END,"起始时间输⼊错误\n")
url ="sswhu/index.php/home/data_product/get_obs_data.html?src=IGS&start_date="+ start_date +"&end_date="+ end_date +"&si tes%5B%5D="+ choose +"&order=site&format=o"
data = s.get(url, headers=headers)
strip()=='':
txt.insert(END,"⼴播星历没有任何数据\n")
return
subject = ,"html.parser")
download_list =[]
for item in subject.select(".site-item"):
# 添加所有符合条件的下载地址
temp = item.select("a")[0]["href"]
download_list.append("wz-igs.oss-cn-beijing.aliyuncs/"+ 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")
chrome直接下载
URL ="sswhu/index.php/home/data_product/get_brdc_data.html?src=IGS&start_date="+ start_date +"&end_date="+ end_date
data = s.get(URL, headers=headers)
strip()=='':
print("⼴播星历没有任何数据")
return
subject = ,"html.parser")
# 下载链接
download_list =[]
for item in subject.select(".am-success"):
# 添加所有符合条件的下载地址
temp = item.select("a")[0]["href"]
download_list.append("wz-igs.oss-cn-beijing.aliyuncs/"+ 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 False
return True
def calc(event):
'''
'''
点击组合框的按钮
:param event: 时间信息
:return:
'''
global choose
choose = select[comb.current()]
def init():
'''
初始化
:return:
'''
url ="sswhu/index.php/home/data_product/igs.html"
data = s.get(url, headers=headers)
subject = ,"html.parser")
for item in subject.select("#sites-selector option"):
select.)
start ="2021-10-01"
end ="2021-10-26"
init()
root = Tk()
root.title('下载程序的窗⼝')
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, (), ()))
btn1.place(relx=0.5, rely=0.4, relwidth=0.2, relheight=0.1)
# ⽅法⼆利⽤ lambda 传参数调⽤run2()
btn2 = Button(root, text='下载⼴播星历', command=lambda: get_broadcast_(), ())) 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)
# fig(command=txt.yview)
# fig(yscrollcommand=scroll.set)
# txt.pack()
txt.insert(END,"程序开始\n")
root.mainloop()
结果如下

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。