emain_time = '{:02d}:{:02d}:{:02d}'.format(int(t_h), int(t_m), int(t_s))
时间: 2024-06-03 12:07:52 浏览: 77
As an AI language model, I cannot provide the complete code as I do not have the context of the variables used in this code snippet. However, I can explain what this line of code does.
This line of code formats the values of the variables t_h, t_m, and t_s to a string that represents the time in the format of hh:mm:ss. The {:02d} formats the integer values with two digits and adds a leading zero if the integer has only one digit. The format() function replaces the curly braces {} with the formatted integer values. The final formatted time string is stored in the variable emain_time.
相关问题
import subprocess import cv2 import numpy as np import time import calendar #从手机获取二进制图片 def get_app_img(): # 从ADB获取屏幕图像 try: output = subprocess.check_output('adb exec-out screencap -p', shell=True) # 处理 output 中的数据 except subprocess.CalledProcessError as e: print('Error:', e) except Exception as e: print('Unexpected error:', e) return output #获取每一张图片的三维数据 def get_imgdecdoe(): output = get_app_img() # print("------output-----{}".format(output)) # 将输出转换为图像 image1 = cv2.imdecode(np.fromstring(output, dtype='uint8'), cv2.IMREAD_COLOR) # print(image) # #缩小图片的大小 image = cv2.resize(image1, (int(1080 / 3), int(2340 / 3))) return image def app_video(): save_path=r"E:\myTool\appium_xiangmu\test_video" ts=calendar.timegm(time.gmtime()) videoname = str(ts)+ ".mp4" save_file_path = '{}\\{}'.format(save_path, videoname) #保存视频 fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 不同视频编码对应不同视频格式(例:'I','4','2','0' 对应avi格式) video = cv2.VideoWriter(save_file_path, fourcc, 5, (int(1080/3),int(2340/3))) try: while True: image = get_imgdecdoe() # 显示图像 cv2.imshow('Screen', image) # 按下ESC键退出循环 if cv2.waitKey(1) == 27: break image=get_imgdecdoe() video.write(image) finally: video.release() #释放 print("________视频处理完毕_______:视频号显示:{}".format(videoname)) cv2.destroyAllWindows() if __name__ == '__main__': app_video(),缺少录制时用户的点击的提示怎么处理,有实例代码吗
如果想要在录制的视频中添加用户的点击提示,可以使用cv2.circle()函数在视频中绘制一个圆圈来表示用户的点击。具体实现可以在获取到每一张图片的时候,判断手机屏幕是否有点击事件发生,如果有,则在该图片上绘制一个圆圈。以下是一个示例代码:
```
import subprocess
import cv2
import numpy as np
import time
import calendar
# 从手机获取二进制图片
def get_app_img():
# 从ADB获取屏幕图像
try:
output = subprocess.check_output('adb exec-out screencap -p', shell=True)
# 处理 output 中的数据
except subprocess.CalledProcessError as e:
print('Error:', e)
except Exception as e:
print('Unexpected error:', e)
return output
# 获取每一张图片的三维数据
def get_imgdecdoe():
output = get_app_img()
# 将输出转换为图像
image1 = cv2.imdecode(np.fromstring(output, dtype='uint8'), cv2.IMREAD_COLOR)
# 缩小图片的大小
image = cv2.resize(image1, (int(1080 / 3), int(2340 / 3)))
return image
# 在图片上绘制圆圈
def draw_circle(image, x, y):
# 设置圆圈的颜色和半径
color = (0, 0, 255)
radius = 10
# 在图片上绘制圆圈
cv2.circle(image, (x, y), radius, color, -1)
def app_video():
save_path=r"E:\myTool\appium_xiangmu\test_video"
ts=calendar.timegm(time.gmtime())
videoname = str(ts)+ ".mp4"
save_file_path = '{}\\{}'.format(save_path, videoname)
#保存视频
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 不同视频编码对应不同视频格式(例:'I','4','2','0' 对应avi格式)
video = cv2.VideoWriter(save_file_path, fourcc, 5, (int(1080/3),int(2340/3)))
try:
while True:
# 获取当前的屏幕图像
image = get_imgdecdoe()
# 判断是否有点击事件发生
# 这里假设点击事件的坐标为 (x, y)
if 点击事件发生:
# 在图片上绘制圆圈
draw_circle(image, x, y)
# 显示图像
cv2.imshow('Screen', image)
# 按下ESC键退出循环
if cv2.waitKey(1) == 27:
break
# 将图片写入视频文件
video.write(image)
finally:
# 释放视频文件
video.release()
print("________视频处理完毕_______:视频号显示:{}".format(videoname))
cv2.destroyAllWindows()
if __name__ == '__main__':
app_video()
```
在上面的代码中,我们定义了一个 `draw_circle()` 函数,用于在图片上绘制圆圈。然后在每次获取到屏幕图像时,判断是否有点击事件发生,如果有,则在该图片上绘制一个圆圈。最后将图片写入视频文件即可。
优化这段代码:import requests import pandas as pd from bs4 import BeautifulSoup from lxml import etree import time import pymysql from sqlalchemy import create_engine from urllib.parse import urlencode # 编码 URL 字符串 start_time = time.time() #计算程序运行时间 def get_one_page(i): try: headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36' } paras = {'reportTime': '2023-03-23', #可以改报告日期,比如2018-6-30获得的就是该季度的信息 'pageNum': i #页码 } url = 'http://s.askci.com/stock/a/?' + urlencode(paras) response = requests.get(url,headers = headers) if response.status_code == 200: return response.text return None except RequestException: print('爬取失败') def parse_one_page(html): soup = BeautifulSoup(html,'lxml') content = soup.select('#myTable04')[0] #[0]将返回的list改为bs4类型 tbl = pd.read_html(content.prettify(),header = 0)[0] # prettify()优化代码,[0]从pd.read_html返回的list中提取出DataFrame tbl.rename(columns = {'序号':'serial_number', '股票代码':'stock_code', '股票简称':'stock_abbre', '公司名称':'company_name', '省份':'province', '城市':'city', '主营业务收入(201712)':'main_bussiness_income', '净利润(201712)':'net_profit', '员工人数':'employees', '上市日期':'listing_date', '招股书':'zhaogushu', '公司财报':'financial_report', '行业分类':'industry_classification', '产品类型':'industry_type', '主营业务':'main_business'},inplace = True) return tbl def generate_mysql(): conn = pymysql.connect( host='localhost', user='root', password='******', port=3306, charset = 'utf8', db = 'wade') cursor = conn.cursor() sql = 'CREATE TABLE IF NOT EXISTS listed_company (serial_number INT(20) NOT NULL,stock_code INT(20) ,stock_abbre VARCHAR(20) ,company_name VARCHAR(20) ,province VARCHAR(20) ,city VARCHAR(20) ,main_bussiness_income VARCHAR(20) ,net_profit VARCHAR(20) ,employees INT(20) ,listing_date DATETIME(0) ,zhaogushu VARCHAR(20) ,financial_report VARCHAR(20) , industry_classification VARCHAR(20) ,industry_type VARCHAR(100) ,main_business VARCHAR(200) ,PRIMARY KEY (serial_number))' cursor.execute(sql) conn.close() def write_to_sql(tbl, db = 'wade'): engine = create_engine('mysql+pymysql://root:******@localhost:3306/{0}?charset=utf8'.format(db)) try: tbl.to_sql('listed_company2',con = engine,if_exists='append',index=False) # append表示在原有表基础上增加,但该表要有表头 except Exception as e: print(e) def main(page): generate_mysql() for i in range(1,page): html = get_one_page(i) tbl = parse_one_page(html) write_to_sql(tbl) # # 单进程 if __name__ == '__main__': main(178) endtime = time.time()-start_time print('程序运行了%.2f秒' %endtime) # 多进程 from multiprocessing import Pool if __name__ == '__main__': pool = Pool(4) pool.map(main, [i for i in range(1,178)]) #共有178页 emdtime = time.time()-start_time print('程序运行了%.2f秒' %(time.time()-start_time))
这段代码已经很简洁了,但是可以通过以下方式进行优化:
1. 导入模块时可以将相同的模块放在一起,如将requests和pandas放在一起,将BeautifulSoup和lxml放在一起。
2. 导入模块时可以只导入需要的部分,如只导入urlencode函数。
3. 可以将函数或代码段封装成函数或类,使代码结构更清晰。
阅读全文