# -*- coding: utf-8 -*-
import time
import uiautomator2 as u2
import requests
from threading import Thread
import re
"""
______________________________
Author: solucky
Email : ssllkkyy@126.com
Time : 2023-6-22 17:40
File: maintest.py
Software: PyCharm
______________________________
"""
'''
运行ui2:
python -m weditor
http://localhost:17310/
'''
# "C:\Users\songlk\AppData\Local\Android\Sdk\tools\bin"
message_list_from_xhs = []
def send_kalulu(d):
d.implicitly_wait(5)
d.xpath('//*[@text="A卡路里"]').click()
time.sleep(2)
d(resourceId='com.tencent.mm:id/b4a').long_click() # 长按点击输入框 弹出粘贴选择
time.sleep(1)
d(resourceId='com.tencent.mm:id/gsd').click() # 点击 粘贴 至输入框
time.sleep(1)
d.xpath('//*[@resource-id="com.tencent.mm:id/b8k"]').click() # 点击 发送
time.sleep(1)
d.xpath('//*[@resource-id="com.tencent.mm:id/yn"]').click() # 点击返回
time.sleep(1)
# "C:\Users\songlk\AppData\Local\Android\Sdk\tools\bin"
def read_kaluli(d):
print("---进入卡路里")
d.implicitly_wait(5)
d.xpath('//*[@text="A卡路里"]').click()
time.sleep(2)
elems = d(resourceId='com.tencent.mm:id/b4b')
print('----1')
for item in elems:
print('----2进入循环遍历获取链接')
item.long_click()
time.sleep(1)
d(resourceId='com.tencent.mm:id/br0').click() # 复制
time.sleep(1)
d(resourceId='com.tencent.mm:id/b4a').long_click() # 长按点击输入框
time.sleep(1)
d(resourceId='com.tencent.mm:id/gsd').click() # 粘贴至输入框
time.sleep(1)
text = d(resourceId='com.tencent.mm:id/b4a', className='android.widget.EditText').get_text()
message_list_from_xhs.append(text)
print(text)
time.sleep(1)
d(resourceId='com.tencent.mm:id/kii').long_click() # 长按点击输入框 弹出全选、剪切选项
time.sleep(1)
d.xpath('//*[@text="全选"]').click_exists() # 点击输入框弹出的全选 后面有弹出剪切选项
time.sleep(1)
d.xpath('//*[@text="剪切"]').click_exists() # 点击输入框弹出剪切
print("---")
d.xpath('//*[@resource-id="com.tencent.mm:id/bxy"]/android.widget.LinearLayout[1]').click()
return message_list_from_xhs
'''
写文件
'''
def write_save(content):
with open('dump.txt', 'a', encoding='utf-8') as f:
f.write(content)
f.write('\n')
'''
进入搜图大师 搜索链接页面
'''
def swipe_d(device):
print("---下滑,进入搜图大师")
# 向下滑
w, h = device.window_size()
device.swipe(w // 2, h // 2, w // 2, h * 4 // 5)
'''
进入卡路里
获取copy内容
'''
def get_copy(d, text):
print("---进入卡路里")
d.implicitly_wait(5)
d.xpath('//*[@text="A卡路里"]').click()
time.sleep(1)
d(resourceId='com.tencent.mm:id/b4a').set_text(text) # 输入框
time.sleep(1)
d(resourceId='com.tencent.mm:id/kii').long_click() # 长按点击输入框 弹出全选、剪切选项
time.sleep(1)
d.xpath('//*[@text="全选"]').click_exists() # 点击输入框弹出的全选 后面有弹出剪切选项
time.sleep(1)
d.xpath('//*[@text="剪切"]').click_exists()
print("微信卡路里中转剪切完毕")
time.sleep(1)
d.xpath('//*[@resource-id="com.tencent.mm:id/bxy"]/android.widget.LinearLayout[1]').click()
'''
进入小红薯小程序 粘贴 解析 复制
copy 小红薯
'''
def copy_soutu_kaluli(d):
swipe_d(d)
d.xpath('//*[@text="搜图大师"]').click()# 等广告
time.sleep(1)
d.click(0.375, 0.899)# 点击下方“视频去水印”
time.sleep(1)
d.click(0.922, 0.394) #点击一下清空
time.sleep(1)
print('进入搜图大师')
d.click(0.425, 0.4) # 点一下 输入框 为了弹出输入法
time.sleep(1)
d.click(0.064, 0.617) # 点击小方格 弹出更多设置
time.sleep(1) #
d.click(0.496, 0.837) #点击 编辑
time.sleep(1)
d.click(0.631, 0.881) # 点击粘贴
time.sleep(1)
d.click(0.729, 0.394) # 点击开始解析
time.sleep(4) #此处根据下载网速 延长时间
d.xpath('//*[@text="复制视频无水印链接"]').click() # 点击复制
time.sleep(1)
d.xpath('//*[@resource-id="com.android.systemui:id/back"]').click() # 点击左下角返回 到搜图小程序首页
time.sleep(1)
d.xpath('//*[@resource-id="com.android.systemui:id/back"]').click() # 点击左下角返回到聊天主页
send_kalulu(d) # 进入卡路里 发送粘贴的地址
'''
从搜图大师页面 输入短连接lists获取可以下载的lists
输入
list_msg=['80 陈乃乃发布了一篇小红书笔记,快来看吧! 😆 4ePVJc4CmsJ6n9L 😆 http://xhslink.com/RR4Gsr,复制本条信息,打开【小红书】App查看精彩内容!', '29 TG发布了一篇小红书笔记,快来看吧! 😆 5H6PUwWm5KNKkIu 😆 http://xhslink.com/nf7Gsr,复制本条信息,打开【小红书】App查看精彩内容!', '29 TG发布了一篇小红书笔记,快来看吧! 😆 5H6PUwWm5KNKkIu 😆 http://xhslink.com/nf7Gsr,复制本条信息,打开【小红书】App查看精彩内容!']
'''
def get_download(device, urls):
print("---get_download")
# device.long_click(0.452, 0.398)
time.sleep(1)
down_avi_urls = []
for index, item in enumerate(urls):
print("寻找url")
device.xpath('//*[@text="请粘贴小红薯链接地址"]').set_text(item)
time.sleep(1)
device.xpath('//*[@text="解析"]').click()
time.sleep(3)
device.xpath('//*[@resource-id="_GB"]').click()
time.sleep(2)
device(resourceId='com.tencent.mm:id/fl').click()
time.sleep(1)
device.xpath('//*[@resource-id="com.android.systemui:id/back"]')
if index == 0:
down_avi_urls.append(item)
return down_avi_urls
return down_avi_urls
def open_device():
print("---start从桌面进入微信---")
device = u2.connect()
device(text='微信').click()
print(device.device_info)
time.sleep(2)
return device
'''
一段话汇总多个http链接返回数组
'''
def get_short_url_list(longtext):
links = re.findall(r"(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)", longtext)
return links
def get_solucky_list(file):
'''
读取文件内容
:return:
'''
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
content_temp = content.replace('\n', '')
content_arr = content_temp.split('solucky:')
content_arr = list(filter(None, content_arr))
print('len={}个 content_arr={} '.format(len(content_arr), content_arr))
return content_arr
def download(url, filename):
response = requests.get(url, stream=True)
print('---download')
with open(filename, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
def multithread_download(video_urls, filenames):
threads = []
for url, filename in zip(video_urls, filenames):
thread = Thread(target=download, args=(url, filename))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
def download_after(filename):
tmplist = []
out_filenames = []
with open(filename, 'r', encoding='utf-8') as f:
content = f.read()
tmplist = get_short_url_list(content)
for i in range(len(tmplist)):
out_filenames.append(r"C:\Users\songlk\Desktop\01{}.mp4".format(i)) # 按照个数命名输出文件的名字
print("要下载的url列表:{}".format(tmplist))
print("输出下载视频文件列表:{}".format(out_filenames))
print("共:{}个".format(len(tmplist)))
multithread_download(tmplist, out_filenames)
def jiexi(my_file_):
list_msg = get_solucky_list(my_file_)
print("获取段链接:{}".format(list_msg))
device = open_device() # 打开微信
for index, item in enumerate(list_msg):
if index < 5: # 修正前面0- (X-1) 不解析了 防止终端
continue
print("--------------------")
print("--------------------")
print("第{}个".format(index))
write_save("第{}个".format(index))
# print("--------------------")
print("--------------------")
get_copy(device, item)
copy_soutu_kaluli(device)
print("正常解析结束")
if __name__ == '__main__':
'''
将指定文件my_file的短链,经过解析粘贴到指定微信列表消息中(卡路里)中
'''
my_file = r'C:\Users\songlk\Desktop\v.txt'
after_downlaod = r'C:\Users\songlk\Desktop\d.txt'
print("---main")
jiexi(my_file)
# download_after(after_downlaod)
最新获取xiaohongshu视频uiautomator2视频
于 2023-07-16 17:40:50 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)