yongyi_pig.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # coding:utf-8
  2. from time import sleep
  3. import datetime
  4. import openpyxl
  5. import requests
  6. from selenium import webdriver
  7. # 设置Chrome浏览器选项
  8. from selenium.common import exceptions
  9. from selenium.webdriver.chrome.service import Service
  10. from selenium.webdriver.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.support.wait import WebDriverWait
  13. from imgcode_ak import image_code
  14. """
  15. 根据table的id属性和table中的某一个元素定位其在table中的位置
  16. table包括表头,位置坐标都是从1开始算
  17. tableId:table的id属性
  18. queryContent:需要确定位置的内容
  19. """
  20. def get_table_content(driver, tableId, queryContent):
  21. # 按行查询表格的数据,取出的数据是一整行,按空格分隔每一列的数据
  22. table_tr_list = driver.find_element(By.ID, tableId).find_elements(By.TAG_NAME, "tr")
  23. table_list = [] # 存放table数据
  24. for tr in table_tr_list: # 遍历每一个tr
  25. # 将每一个tr的数据根据td查询出来,返回结果为list对象
  26. table_td_list = tr.find_elements(By.TAG_NAME, "td")
  27. row_list = []
  28. print(table_td_list)
  29. for td in table_td_list: # 遍历每一个td
  30. row_list.append(td.text) # 取出表格的数据,并放入行列表里
  31. table_list.append(row_list)
  32. # 循环遍历table数据,确定查询数据的位置
  33. # for i in range(len(table_list)):
  34. # for j in range(len(table_list[i])):
  35. # if queryContent == table_list[i][j]:
  36. # print("%r坐标为(%r,%r)" % (queryContent, i + 1, j + 1))
  37. # 写入文件
  38. def write_excel_xlsx(path, sheet_name, value):
  39. index = len(value) # 列表中所含元组的个数,从而确定写入Excel的行数
  40. # 打开Excel
  41. wb = openpyxl.Workbook()
  42. # wb = load_workbook(path)
  43. sheet = wb.active # 获得一个的工作表
  44. sheet.title = sheet_name
  45. # 设置格式
  46. sheet.column_dimensions['B'].width = 115
  47. # 按行加入
  48. for i in range(index):
  49. sheet.append(value[i])
  50. # 保存文件
  51. print(sheet.values)
  52. wb.save(path)
  53. print("题目写入数据成功!")
  54. def send_file(url, file_path):
  55. with open(file_path, 'rb') as file:
  56. files = {'file': file}
  57. response2 = requests.post(url, files=files)
  58. return response2
  59. def get_element(my_driver, xpaths):
  60. """
  61. 判断是否存在元素并获取元素对象
  62. :param my_driver:
  63. :param xpaths: xpaths表达式
  64. :return: 元素对象或为空
  65. """
  66. try:
  67. target = my_driver.find_element(By.XPATH, xpaths)
  68. except exceptions.NoSuchElementException:
  69. return False
  70. else:
  71. return target
  72. if __name__ == "__main__":
  73. # 创建一个 Chrome WebDriver 实例
  74. options = webdriver.ChromeOptions()
  75. # options.add_argument("headless")
  76. # options.add_argument('--headless')
  77. options.add_argument('--disable-gpu')
  78. options.add_argument('--no-sandbox')
  79. options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, '
  80. 'like Gecko) Chrome/118.0.5993.70 Safari/537.36')
  81. options.add_argument(" window-size=1920,1080")
  82. prefs = {'download.prompt_for_download': False, 'download.default_directory': '/Users/xiexiaoyuan/Downloads/'}
  83. options.add_experimental_option('prefs', prefs)
  84. s = Service(executable_path='/Users/xiexiaoyuan/chromedriver_mac64_114/chromedriver')
  85. # s = Service(executable_path='/Users/xi/Desktop/chromedriver')
  86. driver = webdriver.Chrome(service=s, options=options)
  87. # driver.maximize_window()
  88. driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  89. "source": """
  90. Object.defineProperty(navigator, 'webdriver', {
  91. get: () => undefined
  92. })
  93. """
  94. })
  95. driver.get('https://data.yongyizixun888.com/index.php?s=member&c=login&m=index')
  96. sleep(2)
  97. admin_name = ''
  98. admin_pwd = ''
  99. table = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[2]/img')
  100. table.screenshot(r'pig.png')
  101. sleep(2)
  102. # 解析图片验证码
  103. code = image_code('pig.png')
  104. if code == "":
  105. print("获取图片验证码失败")
  106. print(code)
  107. # 模拟登录
  108. # 输入账号
  109. username = driver.find_element(By.XPATH, '//*[@id="myform"]/div[1]/div/input')
  110. username.send_keys(admin_name)
  111. sleep(1)
  112. # 输入密码
  113. password = driver.find_element(By.XPATH, '//*[@id="myform"]/div[2]/div/input')
  114. password.send_keys(admin_pwd)
  115. sleep(1)
  116. # 输入验证码
  117. capter = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[1]/input')
  118. capter.send_keys(code)
  119. sleep(1)
  120. # 登录
  121. signup = driver.find_element(By.XPATH, '//*[@id="myform"]/div[4]/button')
  122. signup.click()
  123. sleep(5)
  124. # 没有登录成功重试
  125. account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text
  126. print(account)
  127. if account != admin_name:
  128. # 解析图片验证码
  129. table = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[2]/img')
  130. table.screenshot(r'pig.png')
  131. code = image_code('pig.png')
  132. if code == "":
  133. print("获取图片验证码失败")
  134. # 输入验证码
  135. capter = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[1]/input')
  136. capter.clear()
  137. capter.send_keys(code)
  138. sleep(1)
  139. # 登录
  140. signup = driver.find_element(By.XPATH, '//*[@id="myform"]/div[4]/button')
  141. signup.click()
  142. sleep(2)
  143. account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text
  144. print(account)
  145. if account == admin_name:
  146. # 跳转首页
  147. driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[3]/ul/li[1]/a').click()
  148. sleep(2)
  149. a = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[2]/a')
  150. print(a.get_attribute("href"))
  151. a.click()
  152. # 下载涌溢完整数据库
  153. sleep(1)
  154. b = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[3]/a')
  155. print(b.get_attribute("href"))
  156. b.click()
  157. sleep(10)
  158. print("下载成功")
  159. else:
  160. print("登录失败")
  161. driver.close()