使用python和adb自动评论回复抖音视频,会有一点不稳定,还在完善中。
首先配置好adb环境变量,手机打开USB调试,连接电脑,cmd命令窗口,输入命令 adb devices,出现下面结果时,adb已连接手机可以使用了。
List of devices attached
80SQBD******N device
代码目录结构:
![图片[1]-利用Python和ADB自动评论抖音视频,YYishare](https://img.yyishare.com/wp-content/uploads/2020/05/douying_20200508151105.png)
不通手机分辨率不同,根据手机分辨率 修改config中的config.json配置。运行fun中reply.py。data.json中配置回复内容
adb命令:
- adb devices 列出已连接的设备 adb shell wm size 获取连接设备的分辨率信息
- adb shell am start -n com.ss.android.ugc.aweme/.splash.SplashActivity 启动设备中的app应用 -n 后面时app包名和启动程序
- adb shell input tap x y 模拟点击操作 x y 是点击的坐标
- adb shell input text content 输入文本 content换成要输入的文本 只能输入英文
- adb shell input swipe x1 y1 x2 y2 模拟滑动操作 从坐标x1 y1 滑动到坐标x2 y2
Adbtools.py:封装adb的一些操作
import os
from time import sleep
class AdbTools():
# 检查设备连接
def check_device(self):
try:
print('检查设备是否连接...')
devices = os.popen('adb devices').read()
devices = devices.split('\n')
while '' in devices:
devices.remove('')
if len(devices) == 1:
print('没有设备连接')
return False
# exit(1)
elif len(devices) > 1:
print('设备已连接:')
for each in devices:
print(each)
return True
except Exception as e:
print('请检查adb环境变量' + str(e))
return False
# 获取屏幕分辨率
def get_screen_size(self):
screen_size = os.popen('adb shell wm size').read()
screen_size = screen_size.split(':')[1].replace(' ','').replace('\r','').replace('\n','')
return screen_size
# 打开抖音app
def open_app(self):
# 启动抖音app
try:
# os.system('adb shell am start -n com.ss.android.ugc.aweme/com.ss.android.ugc.aweme.main.MainActivity')
os.system('adb shell am start -n com.ss.android.ugc.aweme/.splash.SplashActivity')
print("打开抖音app成功")
except :
print("打开抖音app失败")
sleep(3)
# os.system('adb shell input tap 500 500')
def click_operate(self, x, y):
os.system('adb shell input tap {} {}'.format(x, y))
def input_comment(self,cont):
# cmd = 'adb shell am broadcast -a ADB_INPUT_TEXT --es msg {text}'.format(text=cont.replace(' ', '%s'))
# os.system(cmd)
os.system('adb shell input text {}'.format(cont))
def slide_operate(self, x, y, tox, toy):
os.system("adb shell input swipe {} {} {} {} 100".format(x, y, tox, toy))
config.py :获取配置文件中的数据
import json
import os
import sys
from common.Adbtools import AdbTools
# 加载屏幕分辨率配置文件
def get_screen_size_conf():
path = sys.path[1]
screen_size = AdbTools().get_screen_size() # 得到屏幕分辨率
screen_conf_path = path + "\config" # 配置文件目录
screen_size_path = screen_conf_path + "\\" + screen_size +"\\" #对应分辨率的配置目录
if os.path.exists(screen_size_path):
with open(screen_size_path+"/config.json",'r') as f:
print("从{}中加载配置文件".format(screen_size))
return json.load(f)
else:
with open(screen_conf_path+"\default.json",'r') as f:
print("加载默认屏幕分辨率配置文件")
return json.load(f)
def get_comment_data():
path = sys.path[1]
comment_data_path = path + r"\config\data.json"
with open(comment_data_path, 'r', encoding='utf-8') as f:
return json.load(f)
config.json:配置按钮坐标
{
"center_point":{
"x": 540,
"y": 960
},
"comment_bottom":{
"x": 990,
"y": 1120
},
"comment_text":{
"x": 250,
"y": 1840
},
"comment_send":{
"x": 1010,
"y": 840
},
"comment_close": {
"x": 1010,
"y": 350
},
"next_video": {
"x": 540,
"y": 1200,
"tox": 540,
"toy": 500
}
}
reply.py
from time import sleep
from common.Adbtools import AdbTools
from common.config import get_screen_size_conf, get_comment_data
at = AdbTools()
flag = at.check_device()
if flag :
screen_data = get_screen_size_conf()
comment_bottom = screen_data["comment_bottom"]
comment_data = get_comment_data()
comment_text = screen_data["comment_text"]
comment_send_path = screen_data["comment_send"]
comment_close_path = screen_data["comment_close"]
next_video_path = screen_data["next_video"]
at.open_app()
i = 0
while i < 5:
at.click_operate(comment_bottom["x"],comment_bottom["y"])
sleep(2)
at.click_operate(comment_text["x"], comment_text["y"])
sleep(2)
at.input_comment(comment_data["data"])
at.click_operate(comment_send_path["x"], comment_send_path["y"])
at.click_operate(comment_close_path["x"], comment_close_path["y"])
at.slide_operate(next_video_path["x"], next_video_path["y"], next_video_path["tox"], next_video_path["toy"])
i += 1
else:
exit(1)
© 版权声明
THE END
请登录后发表评论
注册