zoukankan      html  css  js  c++  java
  • selenium 批量下载文件,json,重命名

    from selenium import webdriver
    import time
    import json
    import os
    import datetime
    number = 0

    #--------------------------------------------------------------------------------
    # 设置启动默认浏览器,chrome UserData路径
    # profile_dir = r"C:\Users\Administrator\AppData\Local\Google\Chrome\User Data"
    # options = webdriver.ChromeOptions()
    # options.add_argument("--user-data-dir=" + profile_dir)
    # driver = webdriver.Chrome(chrome_options=options, executable_path="C:\Program Files
    # (x86)\Google\Chrome\Application\chromedriver.exe")
    #--------------------------------------------------------------------------------

    # 设置下载路径
    # 尝试过使用图片识别库,来验证验证码,成功率太低,改为手动输入
    # https://segmentfault.com/a/1190000015489113?utm_source=tag-newest
    options = webdriver.ChromeOptions()
    prefs = {'profile.default_content_settings.popups': 0,
    'download.default_directory': 'C:\\Users\Administrator\\Desktop\\requests_pwrd'}
    options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe",chrome_options=options)


    # def get_json():
    # """获取json配置文件中的key和value"""
    # {"a" :"a","a" :"a"}
    # json_file = json.load(open('haiwai_settings.json', 'r'))
    # keys_list = []
    # values_list = []
    # for key, value in json_file.items():
    # keys_list.append(str(key))
    # values_list.append(str(value))
    # return keys_list, values_list


    def get_json():
    '''算出昨天和上月的时间并插入配置'''
    """获取json配置文件中的key和value"""
    time_current = time.time()
    timeArray = time.localtime(time_current-86400)
    time_yesterday = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    time_yesterday = time_yesterday.split()[0]
    timeArray = time.localtime(time_current-2678400)
    time_last_month = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    time_last_month = time_last_month.split()[0]

    # {"a" :"a","a" :"a"}
    json_file = json.load(open('haiwai_settings.json', 'r'))
    keys_list = []
    values_list = []
    for key, value in json_file.items():
    key = key.format(time_last_month, time_yesterday)
    keys_list.append(str(key))
    values_list.append(str(value))
    return keys_list, values_list


    def get_url_file():
    global driver
    driver.maximize_window()
    driver.implicitly_wait(10)
    url_link, values_list = get_json()
    driver.get(url_link[0])
    driver.find_elements_by_xpath('//*[@class="controls"]/input')[0].send_keys('')
    driver.find_elements_by_xpath('//*[@class="controls"]/input')[1].send_keys('')
    veri_code = input('请输入网页中的验证码')
    get_file()


    def get_file():
    global driver
    url_link, values_list = get_json()
    for url in url_link:
    driver.get(url)
    time.sleep(1)
    rename_file()


    def rename_file():
    """重命名"""
    oldname = sort_file()
    newname = new_name()
    dir_link = 'C:\\Users\\Administrator\\Desktop\\requests_pwrd'
    os.chdir(dir_link)
    os.rename(oldname, newname)


    def sort_file():
    """排序文件"""
    dir_link = 'C:\\Users\\Administrator\\Desktop\\requests_pwrd'
    dir_lists = os.listdir(dir_link)
    dir_lists.sort(key=lambda fn: os.path.getmtime(dir_link + '\\' + fn))
    return (dir_lists[-1])


    def new_name():
    global number
    today = datetime.date.today()
    time_newname = str(today.strftime('%y%m%d'))
    '''获取value'''
    url_link, values_list = get_json()
    newname_value = values_list[number]
    number += 1
    return time_newname + newname_value + '.xls'


    if __name__ == '__main__':
    get_url_file()

    ————————————————
    版权声明:本文为CSDN博主「112479」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_18525247/article/details/82389538

  • 相关阅读:
    函数式编程之moand的作用
    函数式编程之构建--待完成
    函数式编程中的组合子
    函数式编程中的函数—函数式编程的多态
    swift函数式编程之compose
    swift的柯里化demo
    trait Monad:函数式编程类型系统本博客搜索关键字--类型升降
    函数式编程的三个核心问题:计算的构建、求值、关系
    金融危机是现有金融体制的重构和资产价格的重估
    代理模式:稳定的代理,可变的实现(替换)
  • 原文地址:https://www.cnblogs.com/javalinux/p/14804788.html
Copyright © 2011-2022 走看看