|
| 1 | +# @Time : 2020/6/9 16:37 |
| 2 | +# @Author : Libuda |
| 3 | +# @FileName: screen_shoot.py |
| 4 | +# @Software: PyCharm |
| 5 | +from selenium.webdriver.common.action_chains import ActionChains |
| 6 | +from selenium.webdriver.support.select import Select |
| 7 | +from selenium import webdriver |
| 8 | +from PIL import Image |
| 9 | +from datetime import datetime, time as dtime |
| 10 | +import time |
| 11 | + |
| 12 | +# 程序休眠时等待时间 |
| 13 | +time_sleep_totle = 300 |
| 14 | +# 截图时等待时间 |
| 15 | +time_sleep = 5 |
| 16 | +out_dir = "./out" |
| 17 | + |
| 18 | + |
| 19 | +def logger(msg): |
| 20 | + """ |
| 21 | + 日志信息 |
| 22 | + """ |
| 23 | + now = time.ctime() |
| 24 | + print("[%s] %s" % (now, msg)) |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + logger("脚本开始执行") |
| 29 | + driver = webdriver.PhantomJS(executable_path="phantomjs.exe") |
| 30 | + driver.maximize_window() |
| 31 | + |
| 32 | + left, top, right, bottom = 632, 528, 1183, 1343 |
| 33 | + driver.get("http://data.eastmoney.com/futures/sh/data.html?ex=069001005&va=RB") |
| 34 | + # 交易所选择 |
| 35 | + jiaoyisuo_select = driver.find_element_by_id("futures_exchange") |
| 36 | + jiaoyisuo_options = Select(jiaoyisuo_select).options |
| 37 | + for i in range(len(jiaoyisuo_options)): |
| 38 | + logger("选择交易所:{}".format(jiaoyisuo_options[i].text)) |
| 39 | + # 交易下拉框 |
| 40 | + ActionChains(driver).move_to_element(jiaoyisuo_select).perform() |
| 41 | + Select(jiaoyisuo_select).select_by_index(i) |
| 42 | + |
| 43 | + # 选择品种 |
| 44 | + pingzhong_select = driver.find_element_by_id("futures_variety") |
| 45 | + pingzhong_options = Select(pingzhong_select).options |
| 46 | + |
| 47 | + for j in range(len(pingzhong_options)): |
| 48 | + logger("选择品种:{}".format(pingzhong_options[j].text)) |
| 49 | + ActionChains(driver).move_to_element(pingzhong_select).perform() |
| 50 | + Select(pingzhong_select).select_by_index(j) |
| 51 | + |
| 52 | + logger("当前交易所:{},品种:{}".format(jiaoyisuo_options[i].text, pingzhong_options[j].text)) |
| 53 | + # 点击查询 |
| 54 | + driver.find_element_by_class_name("btn").click() |
| 55 | + time.sleep(time_sleep) |
| 56 | + driver.save_screenshot('capture.png') # 截取全屏 |
| 57 | + |
| 58 | + # ele1 = driver.find_element_by_xpath('//*[@id="mainContent"]/div[2]') |
| 59 | + # ele2 = driver.find_element_by_xpath('//*[@id="mainContent"]/div[7]') |
| 60 | + # 获取元素位置信息 |
| 61 | + # left = ele1.location['x']-5 |
| 62 | + # top = ele1.location['y'] |
| 63 | + # right = left + ele1.size['width']*2+15 |
| 64 | + # bottom = top + ele1.size['height']+ele2.size['height']+10 |
| 65 | + # print(left,top,right,bottom) |
| 66 | + |
| 67 | + im = Image.open('capture.png') |
| 68 | + im = im.crop((left, top, right, bottom)) # 元素裁剪 |
| 69 | + img_name = pingzhong_options[j].text + "持仓报告.png" |
| 70 | + im.save("./out/" + img_name) # 元素截图 |
| 71 | + |
| 72 | + # print("截图保存成功") |
| 73 | + driver.quit() |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + |
| 78 | + DAY_START = dtime(21, 0) |
| 79 | + DAY_END = dtime(21, 20) |
| 80 | + |
| 81 | + NIGHT_START = dtime(21, 0) |
| 82 | + NIGHT_END = dtime(21, 20) |
| 83 | + |
| 84 | + while 1: |
| 85 | + t = time.gmtime() |
| 86 | + # 判断是否是星期天 |
| 87 | + if t.tm_wday == 5 or t.tm_wday == 6: |
| 88 | + logger("星期天不执行脚本") |
| 89 | + time.sleep(time_sleep_totle) |
| 90 | + continue |
| 91 | + else: |
| 92 | + current_time = datetime.now().time() |
| 93 | + if DAY_START <= current_time <= DAY_END or (NIGHT_START <= current_time <= NIGHT_END): |
| 94 | + # 判断时候在可运行时间内 |
| 95 | + logger("等待截图中") |
| 96 | + try: |
| 97 | + main() |
| 98 | + time.sleep(time_sleep_totle) |
| 99 | + except Exception as e: |
| 100 | + print("运行错误", e) |
| 101 | + time.sleep(time_sleep_totle) |
| 102 | + else: |
| 103 | + logger("其他时间段 其他任务执行中") |
| 104 | + time.sleep(time_sleep_totle) |
0 commit comments