Advertisement

下载费米卫星数据

阅读量:
复制代码
 import requests

    
 from lxml import etree
    
 import os
    
 import pickle
    
 ########################################################################################################################
    
 def makeDir(self,dir):
    
     """
    
     创建目录,首先判断当前目录下是否存在活目录名字,存在就直接转入,不然就创建之后再转入
    
     """
    
     if os.path.exists(dir):
    
     os.chdir(dir)
    
     else:
    
     os.mkdir(dir)
    
     os.chdir(dir)
    
 def parentDir():
    
     """
    
     返回当前目录的上一层,上一层目录的绝对路径为当前目录路径加..
    
     """
    
     print("当前目录为{}".format(os.getcwd()))
    
     parentPath=os.path.abspath((os.path.join(os.getcwd(),"..")))
    
     os.chdir(parentPath)
    
     print("当前目录为{}".format(os.getcwd()))
    
 def getResponse(url,timeout):
    
     headers={"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0"}
    
     try:
    
     response=requests.get(url=url,headers=headers,timeout=timeout)
    
     return response
    
     except:
    
     print("timeout++++++++++++++++++++++++"+url)
    
     return 0
    
  
    
 ########################################################################################################################
    
 class FermiMonitored():
    
     def __init__(self):
    
     self.url='https://fermi.gsfc.nasa.gov/ssc/data/access/lat/msl_lc/'
    
     self.headers={"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0"}
    
     def getAllName(self):
    
     """获取所有Fermi监测天体的名称"""
    
     response=getResponse(url=self.url,timeout=30)
    
     if response!=0:
    
         html=etree.HTML(response.text)
    
         name=html.xpath('//td[@rowspan="2"]/a/text()')
    
         with open('allSourceName.pkl','wb') as file:
    
             pickle.dump(name,file)
    
         print("所有监测源名称保存在allSourceName.pkl文件中")
    
 if __name__=="__main__":
    
     # allSourceName=FermiMonitored().getAllName()
    
     with open('allSourceName.pkl','rb') as file:
    
     name=pickle.load(file)
    
     print(name)

全部评论 (0)

还没有任何评论哟~