# coding:utf-8 from time import sleep import datetime import openpyxl import requests from selenium import webdriver # 设置Chrome浏览器选项 from selenium.common import exceptions from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from imgcode_ak import image_code """ 根据table的id属性和table中的某一个元素定位其在table中的位置 table包括表头,位置坐标都是从1开始算 tableId:table的id属性 queryContent:需要确定位置的内容 """ def get_table_content(driver, tableId, queryContent): # 按行查询表格的数据,取出的数据是一整行,按空格分隔每一列的数据 table_tr_list = driver.find_element(By.ID, tableId).find_elements(By.TAG_NAME, "tr") table_list = [] # 存放table数据 for tr in table_tr_list: # 遍历每一个tr # 将每一个tr的数据根据td查询出来,返回结果为list对象 table_td_list = tr.find_elements(By.TAG_NAME, "td") row_list = [] print(table_td_list) for td in table_td_list: # 遍历每一个td row_list.append(td.text) # 取出表格的数据,并放入行列表里 table_list.append(row_list) # 循环遍历table数据,确定查询数据的位置 # for i in range(len(table_list)): # for j in range(len(table_list[i])): # if queryContent == table_list[i][j]: # print("%r坐标为(%r,%r)" % (queryContent, i + 1, j + 1)) # 写入文件 def write_excel_xlsx(path, sheet_name, value): index = len(value) # 列表中所含元组的个数,从而确定写入Excel的行数 # 打开Excel wb = openpyxl.Workbook() # wb = load_workbook(path) sheet = wb.active # 获得一个的工作表 sheet.title = sheet_name # 设置格式 sheet.column_dimensions['B'].width = 115 # 按行加入 for i in range(index): sheet.append(value[i]) # 保存文件 print(sheet.values) wb.save(path) print("题目写入数据成功!") def send_file(url, file_path): with open(file_path, 'rb') as file: files = {'file': file} response2 = requests.post(url, files=files) return response2 def get_element(my_driver, xpaths): """ 判断是否存在元素并获取元素对象 :param my_driver: :param xpaths: xpaths表达式 :return: 元素对象或为空 """ try: target = my_driver.find_element(By.XPATH, xpaths) except exceptions.NoSuchElementException: return False else: return target if __name__ == "__main__": # 创建一个 Chrome WebDriver 实例 options = webdriver.ChromeOptions() # options.add_argument("headless") # options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument('--no-sandbox') options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, ' 'like Gecko) Chrome/118.0.5993.70 Safari/537.36') options.add_argument(" window-size=1920,1080") prefs = {'download.prompt_for_download': False, 'download.default_directory': '/Users/xiexiaoyuan/Downloads/'} options.add_experimental_option('prefs', prefs) s = Service(executable_path='/Users/xiexiaoyuan/chromedriver_mac64_114/chromedriver') # s = Service(executable_path='/Users/xi/Desktop/chromedriver') driver = webdriver.Chrome(service=s, options=options) # driver.maximize_window() driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ }) driver.get('https://data.yongyizixun888.com/index.php?s=member&c=login&m=index') sleep(2) admin_name = '' admin_pwd = '' table = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[2]/img') table.screenshot(r'pig.png') sleep(2) # 解析图片验证码 code = image_code('pig.png') if code == "": print("获取图片验证码失败") print(code) # 模拟登录 # 输入账号 username = driver.find_element(By.XPATH, '//*[@id="myform"]/div[1]/div/input') username.send_keys(admin_name) sleep(1) # 输入密码 password = driver.find_element(By.XPATH, '//*[@id="myform"]/div[2]/div/input') password.send_keys(admin_pwd) sleep(1) # 输入验证码 capter = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[1]/input') capter.send_keys(code) sleep(1) # 登录 signup = driver.find_element(By.XPATH, '//*[@id="myform"]/div[4]/button') signup.click() sleep(5) # 没有登录成功重试 account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text print(account) if account != admin_name: # 解析图片验证码 table = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[2]/img') table.screenshot(r'pig.png') code = image_code('pig.png') if code == "": print("获取图片验证码失败") # 输入验证码 capter = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[1]/input') capter.clear() capter.send_keys(code) sleep(1) # 登录 signup = driver.find_element(By.XPATH, '//*[@id="myform"]/div[4]/button') signup.click() sleep(2) account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text print(account) if account == admin_name: # 跳转首页 driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[3]/ul/li[1]/a').click() sleep(2) a = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[2]/a') print(a.get_attribute("href")) a.click() # 下载涌溢完整数据库 sleep(1) b = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[3]/a') print(b.get_attribute("href")) b.click() sleep(10) print("下载成功") else: print("登录失败") driver.close()