def get_stock_minute_price_in_one_day_by_sina_pyautogui_new(str_date,tscode): newdf={} x = 81 y = 635 while True: try: pyautogui.click(pyautogui.center(pyautogui.locateOnScreen(r"C:\PycharmProjects\my_backtest\img\browser.png", confidence=0.9)),duration=0.25) waiting_time=time.time() while True: if(pyautogui.locateOnScreen(r"C:\PycharmProjects\my_backtest\img\browser_plus.png", confidence=0.8)!=None): pyautogui.click(pyautogui.center( pyautogui.locateOnScreen(r"C:\PycharmProjects\my_backtest\img\browser_plus.png", confidence=0.8)), duration=0.25) break if(time.time()-waiting_time>10): raise BaseException time.sleep(1) pyautogui.typewrite("https://finance.sina.com.cn/h5charts/tchart.html?symbol="+tscode+"&date="+str_date+"&rangeselector=true&indicator=tvol",interval=0.05) time.sleep(1) pyautogui.press("enter") time.sleep(2) # if(pyautogui.locateOnScreen(r"C:\PycharmProjects\my_backtest\img\void_line.png", confidence=0.8)==None): # raise BaseException pyautogui.moveTo(x, y, duration=0.25) while True: x=x+7 pyautogui.moveTo(x,y) img1 = pyautogui.screenshot(region=(2279 - 24,237- 66, 214, 154)) img2 = pyautogui.screenshot(region=(117 - 24, 238 - 66, 214, 154)) pool.submit(gsmpiodbspn_thread, newdf, img1,img2) if (("15:00" in newdf) == True or x >= 2475): break pyautogui.click(pyautogui.center( pyautogui.locateOnScreen(r"C:\PycharmProjects\my_backtest\img\close_browser.png", confidence=0.8)), duration=0.25) if(("15:00" in newdf)==True or x>=2475): break except BaseException as e: print(e) pyautogui.click(pyautogui.center( pyautogui.locateOnScreen(r"C:\PycharmProjects\my_backtest\img\close_browser.png", confidence=0.8)), duration=0.25) return newdf。 请问上面这段个函数,在不改变原来功能的情况下,如何提高运行速度
时间: 2023-11-09 15:05:49 浏览: 128
这段代码的运行速度可能受到以下因素的影响:
1. 浏览器的启动和关闭时间
2. 截屏和图像处理的时间
3. 等待时间的设置
可以尝试优化这些因素以提高代码的运行速度,具体方法如下:
1. 尝试使用无头浏览器,如Selenium或Puppeteer,以避免启动和关闭浏览器的时间消耗。
2. 考虑使用更快速的图像处理库,如OpenCV,以减少截屏和图像处理的时间。
3. 适当调整等待时间,以确保页面加载完成,但不要设置过长的等待时间。
另外,可以使用多线程或异步编程来提高代码的并发性和运行速度。但在使用多线程或异步编程时,需要注意线程安全和并发控制问题,以避免出现竞态条件或其他错误。
相关问题
import datetime # a = datetime.datetime.now() def day_get(d): if type(d).__name__ == "str": d = datetime.datetime.strptime(d, '%Y-%m-%d %H:%M:%S') oneday = datetime.timedelta(days=1) day = d - oneday date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0) date_to = datetime.datetime(day.year, day.month, day.day, 23, 59, 59) print('---'.join([str(date_from), str(date_to)])) def week_get(d): if type(d).__name__ == "str": d = datetime.datetime.strptime(d, '%Y-%m-%d %H:%M:%S') dayscount = datetime.timedelta(days=d.isoweekday()) dayto = d - dayscount sixdays = datetime.timedelta(days=6) dayfrom = dayto - sixdays date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 0, 0, 0) date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59) datelist = [[str(date_from)], [str(date_to)]] # print('---'.join([str(date_from), str(date_to)])) return datelist def multi_week_get(d, num): if type(d).__name__ == "str": d = datetime.datetime.strptime(d, '%Y-%m-%d %H:%M:%S') date_num = [] # date_num = [date_to1] for i in range(num - 1, 0, -1): dayscount = datetime.timedelta(days=d.isoweekday()) dayto = d - dayscount sixdays = datetime.timedelta(days=6 * i) dayfrom = dayto - sixdays date_from = str(datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 10, 0, 0)) date_num.append(date_from) dayscount1 = datetime.timedelta(days=d.isoweekday()) onedays = datetime.timedelta(days=1) dayto1 = d - dayscount1 + onedays date_to1 = str(datetime.datetime(dayto1.year, dayto1.month, dayto1.day, 10, 0, 0)) date_num.append(date_to1) return date_num def month_get(d): if type(d).__name__ == "str": d = datetime.datetime.strptime(d, '%Y-%m-%d %H:%M:%S') dayscount = datetime.timedelta(days=d.day) dayto = d - dayscount date_from = datetime.datetime(dayto.year, dayto.month, 1, 0, 0, 0) date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59) # print('---'.join([str(date_from), str(date_to)])) datelist = [[str(date_from)], [str(date_to)]] return datelist class op_date(object): def week_get(self, param): pass if __name__ == "__main__": op_date = op_date() # print(op_date.week_get("2023-06-09 23:00:00")[0]) print(op_date.week_get("2023-06-09 23:00:00")[1]) print(multi_week_get("2023-06-09 23:00:00", 4))
这段代码是关于日期时间的操作,包括获取前一天、上一周、上几周、上个月的起止时间。其中,day_get函数获取前一天的起止时间,week_get函数获取上一周的起止时间,multi_week_get函数获取上几周的起始时间,month_get函数获取上个月的起止时间。op_date类是一个日期时间操作的类,其中week_get是一个占位函数,可以在后续的开发中被具体实现。在代码的最后,通过实例化op_date对象并调用其中的函数来进行测试。
阅读全文