大家好,我是十年码农大兵,目前是一位RPA定制开发者,计划帮忙开发并开源20个RPA机器人项目,如果您有rpa或相关自动化开发需求,欢迎发私信和我交流。
本次开源的RPA项目是:京东视频采集
如需要指定达人京东视频采集可以点击查看我这个文章
《通过影刀rpa实现达人京东视频采集》
需求描述
使用影刀RPA模拟人工在京东APP上浏览视频,获取视频地址再通过浏览器下载视频到电脑,具体采集流程如下:
1、影刀控制手机端自动刷京东精选的家电视频提取分享的视频链接
2、电脑用浏览器打开视频链接提取商品购物地址及视频地址,视频进行下载保存到电脑指定目录
3、将提取的视频名称、商品名称、商品链接、视频下载地址存至excel表格
开发环境:
软件:
影刀ShadowBot-5.20.22-x64.exe
设备:
小米手机note3
手机app:
京东APPV13.2.9 build100046
浏览器:谷歌浏览器(需安装影刀插件)
excel模版
应用分享地址:
影刀应用:
https://api.winrobot360.com/redirect/robot/share?inviteKey=c5a3ffa86bf5af27
密码:vw#ios520888
功能实现:
影刀新增应用
选择手机自动化
新建python模块
复制以下的源码到新建的python模块module1.py文件中
# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能,如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块
import xbot
from xbot import print, sleep
from .import package
from .package import variables as glv
from xbot.mobile import appium
from xbot import excel
from datetime import datetime
import random
import os
from xbot import word
from xbot import web
import urllib.request
import shutil
import re
def sanitize_filename(filename):
"""
清理文件名中的特殊字符。
这里简单地用下划线替换了一些常见的特殊字符,你可以根据需要修改这个函数。
"""
# 注意:这个替换列表可能不完整,根据你的需要添加更多替换
return filename.replace('/', '_').replace('\\', '_').replace(':', '_').replace('?', '_').replace('*', '_').replace('"', '_').replace('<', '_').replace('>', '_').replace('|', '_')
def download(url,path,name):
name = sanitize_filename(name)
filepath = os.path.join(path, name)
with urllib.request.urlopen(url) as response, open(filepath, 'wb') as out_file:
data = response.read() # 读取所有数据到内存(对于大文件可能不是最佳选择)
out_file.write(data) # 写入文件
def find_by_text(session,text):
elements = session.find_all_by_label_name('android.widget.TextView')
for t in range(len(elements)):
element = elements[t]
str = element.get_text()
if(str == text):
return element
return None
def find_text_plus(session,rid,text):
elements = session.find_all_by_id(rid)
for t in range(len(elements)):
name = elements[t].get_text()
if(name == text):
return elements[t]
return None
def openJdApp(session):
print("打开京东APP")
session.open_app("com.jingdong.app.mall")
sleep(2)
element = find_by_text(session,"逛")
element.click()
sleep(2)
print("选择精选-家电")
element = find_text_plus(session,"com.jd.lib.Discovery.feature:id/mz","精选")
element.click()
sleep(5)
element = find_text_plus(session,"com.jd.lib.Discovery.feature:id/sp","家电")
element.click()
sleep(3)
elements = session.find_all_by_id("com.jd.lib.Discovery.feature:id/o_")
element = elements[0]
element.click()
sleep(3)
def getAppVideoUrl(session):
element = session.find_by_id("com.jd.lib.Discovery.feature:id/ug")
element.click()
sleep(1)
#右滑动
session.swipe(930, 1669, 78, 1669)
sleep(1)
element = find_text_plus(session,"com.jingdong.app.mall:id/dx","复制链接")
element.click()
sleep(1)
urlStr = session.get_clipboard_text()
print(urlStr)
start_index = urlStr.find('https')
# 查找结束点:“「”的索引
end_index = urlStr.find('「')
if start_index != -1 and end_index != -1:
urlStr = urlStr[start_index:end_index]
urlStr = urlStr.strip()
print(urlStr)
return urlStr
def writeRow(worksheet,index,videoName,shopName,shopUrl,videoUrl,downloadUrl,videoPath):
# 获取当前时间
current_datetime = datetime.now()
# 使用正确的格式字符串来格式化时间
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
worksheet.set_cell(index, 'A', videoName)
worksheet.set_cell(index, 'B', shopName)
worksheet.set_cell(index, 'C', shopUrl)
worksheet.set_cell(index, 'D', videoUrl)
worksheet.set_cell(index, 'E', downloadUrl)
worksheet.set_cell(index, 'F', videoPath)
worksheet.set_cell(index, 'G', formatted_datetime)
def getWebvideo(worksheet,index,downPath,videoUrl):
print("浏览器获取视频信息及下载视频")
videoPath = datetime.now().strftime("%Y-%m-%d")
browser = web.create(videoUrl, 'chrome', load_timeout=20)
sleep(5)
#html = browser.get_html()
#print(html)
web_element = browser.find_by_xpath('//video')
#print(web_element)
#print(web_element.get_attribute("src"))
downlodUrl = web_element.get_attribute("src")
web_element = browser.find_all_by_css('.shopbag-item-skutitle')[0]
shopName = web_element.get_text()
web_element = browser.find_all_by_css('.desc-videotitle')[0]
videoName = web_element.get_text()
web_element.click()
sleep(2)
shopUrl = browser.get_url()
print("采集视频:"+videoName)
download(downlodUrl,downPath,videoName+".mp4")
writeRow(worksheet,index,videoName,shopName,shopUrl,videoUrl,downlodUrl,videoPath)
browser.close()
def main(args):
current_datetime = datetime.now().strftime("%Y%m%d%H%M%S")
basePath = "C:\\京东视频采集"
videoPath = datetime.now().strftime("%Y-%m-%d")
downPath = basePath+"\\"+videoPath
if not os.path.exists(downPath):
os.makedirs(downPath)
print(f"创建了目录: {downPath}")
excelPath = basePath+"\\"+current_datetime+"_视频采集情况.xlsx"
#复制模版
shutil.copy(basePath+"\\模板\\京东.xlsx",excelPath)
workbook = excel.open(excelPath, kind='auto_check', visible=True)
worksheet = workbook.get_sheet_by_index(1)
session = appium.connect_by_custom_name('Mi Note 3')
openJdApp(session)
for i in range(5):
videoUrl = getAppVideoUrl(session)
index = i+2
getWebvideo(worksheet,index,downPath,videoUrl)
session.swipe(500, 1200, 500, 400)
sleep(2)
workbook.save()
workbook.close()
电脑连接手机,修改相关源码的视频存储路径、excel模板、要连接的手机设备名称,点击运行