栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python-selenium】代码笔记

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python-selenium】代码笔记

# 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库

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/829064.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号