# coding = utf-8
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
'''
This is a python code which mainly use the python-selenium code to autodownload datas and images.
For more infomation about the structure, you can contact me at csdn: XU Hongduo
'''
def download(url,user,password,place,box):
# 1、更换下载地址
prefs = {'profile.default_content_settings.popups': 0,
'download.default_directory': place} # 设置下载文件存放路径,这里要写绝对路径
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', prefs)
# 2、打开网站
browser = webdriver.Chrome(executable_path="C:Program FilesGoogleChromeApplicationchromedriver.exe", options=options)
browser.get(url)
# 3、浏览器最大化
browser.maximize_window()
time.sleep(0.5)
# 4、输入用户名密码
browser.find_element_by_id("username").send_keys(user)
browser.find_element_by_id("password").send_keys(password)
browser.find_element_by_xpath("/ html / body / div[2] / section[1] / form / p[8] / input").click()
print("登录成功!")
# 等待登录成功
time.sleep(20)
# 5、开始循环下载
for i in box:
get_position = "/html/body/main/table/tbody/tr[" + str(i) + "]/td[2]/a"
print ("开始下载数据",i)
position = browser.find_element_by_xpath(get_position)
ActionChains(browser).click(position).perform()
time.sleep(600)
if __name__ == '__main__':
month = "05"
number = 820
url = "https://asdc.larc.nasa.gov/data/CALIPSO/LID_L1-Standard-V4-11/2021/"+month+"/"
user = "test"
password = "test"
place = "Z:\datas\"+month+"\"
# download
box = []
a = 125
for i in range(number-3+1):
box.append(a)
a = a + 1
download(url,user,password,place,box)
注:要先行下载符合当前电脑chrome或firefox浏览器版本的driver.exe执行文件,自行安装selenium库



