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