# 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__": # python+selunium定位已打开的浏览器 # 创建一个 Chrome WebDriver 实例 options = webdriver.ChromeOptions() # options.add_argument("headless") # options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument('--no-sandbox') # 谷歌浏览器运行的默认调试端口:先用以下命令启动浏览器 # 找到谷歌浏览器的程序地址,开启一个新的端口,并设置一个文件夹来保存浏览器的数据 # /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --user-data-dir=/Users/xiexiaoyuan/data/selenium/automationProfile # --remote-debugging-port值,可以指定任何打开的端口 # --user-data-dir标记,指定创建新Chrome配置文件的目录。它是为了确保在单独的配置文件中启动chrome,不会污染你的默认配置文件。 # # 此时会打开一个浏览器页面,我们输入目标网址,输入账号密码,登录成功。 # 登录之后,以后都不需要登录,它会把你这次登录的信息记入到 --user-data-dir指定的目录下 # 后面你只需要python + selenium + webdriver定位到这个已经登录的浏览器进行操作就可以啦 options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # 修改下载地址 options.add_argument("--download.default_directory=/Users/xiexiaoyuan/Downloads/") 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") 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/') sleep(3) account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text print(account) # 下载涌溢日度数据库 sleep(1) 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) # WebDriverWait(driver, 10).until( # EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[2]/a'))).click()