自动上传淘宝达人视频

如果存在有的标签点击事件有时候可行,有时候不行,那就try:except:反复尝试,就可以了

#coding:utf-8

import requests
import re

import json
from selenium import webdriver
import ssl
from lxml import etree
import time
from selenium.webdriver.common.keys import Keys
import os
import shutil
from document import Get_Document
import random

opt = webdriver.ChromeOptions()
# opt.add_argument('--headless')
#更换头部
ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
opt.add_argument('user-agent="%s"' % ua)
# opt.add_argument("--start-maximized")
# opt.add_argument('--start-fullscreen')
driver = webdriver.Chrome(chrome_options=opt)
driver.maximize_window()
class QIE:
    def __init__(self):
        # 上传异常计数,超过阈值放弃当前上传的视频
        self.path0 = input('请输入路径比如:/Users/admin/Downloads/1.5.0/output')
        self.upload_error_handling_num = 0
        jinhao = self.get_jinhao()
        keywords = jinhao['tags']['关键字']
        get_document = Get_Document(keywords)
        self.document_list = get_document.get_index()

        # self.goods_url = 'https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-17476266822.26.30bb442281OvCw&id=573254977409'
        self.title = 'JCC休闲宽松短裤'
        self.title = keywords
        # self.title = input('请输入您的文案标题比如(JCC休闲宽松短裤):')
        # self.detail = 'JCC 阔腿五分裤,轻松显现大长腿。高腰设计,大肚腩、问题腿型统统不见。能够更加突出纤细的美腿,露出小腿的同时,精致立体的车线工艺也能修饰腿形,更显纤美。'
        # self.detail = input('请输入您的文案描述:')
        self.tag = ['爱逛街']
        self.tag.append(keywords)
        # self.tag.append(input('请输入一个标签,比如(五分裤)'))
        # self.video_5s = 'C:/Users/shixiaolong/Desktop/daren/5s.mp4'
        # self.video_5s = input('请输入5s视频的完整路径(比如C:/Users/shixiaolong/Desktop/daren/5s.mp4):')
    def get_detail(self):
        a = random.choice(self.document_list)
        b = random.choice(self.document_list)
        c = random.choice(self.document_list)
        detail = a+b+c
        return detail
    def get_jinhao(self):
        # path0 = 'C:/Users/shixiaolong/Desktop/1.4.0/output'

        path0 = self.path0
        files = os.listdir(path0)
        goods = []
        for i in files:
            try:

                int(i)
                path1_ = path0 + '/' + i
                goods.append(path1_)
            except:
                pass
        try:
            good = goods[0]
            with open(path0 + '/' + 'config.txt') as f:
                tags = f.read()
                tags = json.loads(tags)
            info = {'tags': tags, 'path': good}
            print(info)
            return info
        except:
            return 'no'
    def get_detailinfo(self):
        pass
        # {"匹配时间": "夏", "领域": "服饰", "人群": "女性", "关键字": "连衣裙"}





    def get_video_urls(self):
        self.upload_url = "https://we.taobao.com/material/mine/video?type=ivideo&showDialog=uploadVideo&interactType=chooseVideo"
        self.index_page = 'https://we.taobao.com/publish/post?activityId=68&redirectURL=%2F%2Fwe.taobao.com%2Fsubmission%2Fchannel%2Fmanage&spm=a2116r.submission-channel-68.main.2.4a505df1JptmBq&template=video'
        driver.get(self.upload_url)
        driver.implicitly_wait(10)
        driver.delete_all_cookies()
        try:
            with open('cookies.txt') as f:
                cookies = f.read()
            print(cookies)
            cookies = eval(cookies)
            for co in cookies:
                driver.add_cookie(co)
            driver.get(self.upload_url)
            driver.implicitly_wait(10)
        except:
            cookies = [{'name': 'thw', 'value': 'cn'}, {'name': 't', 'value': 'f7d57de40116a5405fb73c8b6b788af7'},
                       {'name': 'cna', 'value': 'ktaGE5FKzjICAWVafJGQLdrr'},
                       {'name': 'hng', 'value': 'CN%7Czh-CN%7CCNY%7C156'}, {'name': 'tg', 'value': '0'},
                       {'name': 'enc','value': '9ETnzbLmRgM9%2FHxp3ZQ2JKI9sWdzzaLFllkEKYesoftc2B953DpaFdjkHI0ZcNKeCiHyj9X2OfxGx%2FBbGIcY9g%3D%3D'},
                       {'name': 'cookie2', 'value': '35344348c549eb147f7ab42448b7ec24'},
                       {'name': '_tb_token_', 'value': '53ee516013313'},
                       {'name': 'XSRF-TOKEN', 'value': 'b3dd03b8-ecf3-408f-ac64-1490a4538716'},
                       {'name': 'miniDialog', 'value': 'true'}, {'name': 'v', 'value': '0'},
                       {'name': 'unb', 'value': '2028260655'}, {'name': 'sg', 'value': '452'},
                       {'name': '_l_g_', 'value': 'Ug%3D%3D'},
                       {'name': 'cookie1', 'value': 'VFQiCPthG0tVe8Gr7pVeoJzSQzYm55OuaB4ognZhkcs%3D'},
                       {'name': 'tracknick', 'value': '%5Cu82B1%5Cu843D%5Cu65E0%5Cu606F1234'},
                       {'name': 'lgc', 'value': '%5Cu82B1%5Cu843D%5Cu65E0%5Cu606F1234'},
                       {'name': 'dnk', 'value': '%5Cu82B1%5Cu843D%5Cu65E0%5Cu606F1234'},
                       {'name': '_nk_', 'value': '%5Cu82B1%5Cu843D%5Cu65E0%5Cu606F1234'},
                       {'name': 'cookie17', 'value': 'UUjTQgg01Zpmcw%3D%3D'}, {'name': 'uc1', 'value': 'cookie16'},
                       {'name': 'skt', 'value': '3ca8ad7487861b66'}, {'name': 'csg', 'value': '4b7bc750'},
                       {'name': 'uc3', 'value': 'vt3'}, {'name': 'existShop', 'value': 'MTUzMTU0MjcwNQ%3D%3D'},
                       {'name': '_cc_', 'value': 'W5iHLLyFfA%3D%3D'}, {'name': 'mt', 'value': 'ci'},
                       {'name': 'l', 'value': 'AnZ2mDOj0IsL6-wz/c1JgFgnRqd4kbrR'},
                       {'name': '_m_h5_tk', 'value': '8aef291cc3cc60afddc160f52dcb214e_1531545364362'},
                       {'name': '_m_h5_tk_enc', 'value': 'cccad7dfd17689365c6db1f2f044dc2c'},
                       {'name': 'isg', 'value': 'BHNzLYcBLFPNpOAHaeyfZzVJAnFdAAYNsyqmnCUQzRLJJJPGrXiXutGe2xRvrl9i'},
                       {'name': 'apushcd7a07ad5f9abeaf3dd06212069e3d23','value': '%7B%22ts%22%3A1531542776610%2C%22parentId%22%3A1531542712656%7D'}]

        # for co in cookies:
        #     driver.add_cookie(co)
        # driver.get(self.upload_url)
        # driver.implicitly_wait(10)

        # print('ok')
        while True:
            try:
                driver.implicitly_wait(10)
                driver.find_element_by_xpath('//a[@href="/message"]')
                break
            except:
                # try:
                # 定位当前窗口
                # driver.switch_to_window(driver.window_handles[1])
                driver.current_window_handle
                # driver.find_element_by_xpath('//button[contains(./text(),"通过手机安全登录")]').click()
                # except:
                #     pass
                driver.set_window_size(1300, 760)
                path = 'saoma.png'
                driver.save_screenshot(path)
                os.system('start {}'.format(path))
                print('请扫码登录')
                time.sleep(10)
        print('登录成功!')
        cookie = driver.get_cookies()
        with open('cookies.txt','w') as f:
            f.write(str(cookie))

    # 文件导入,上传
    def upload(self,video_5s,video_30s,image,goods_url,field,people,times):
        self.get_video_urls()
        # 将本地文件上传到页面的input标签
        # self.video_30s = 'C:/Users/shixiaolong/Desktop/daren/30s.mp4'
        # self.video_30s = input('请输入长视频的完整路径比如(C:/Users/shixiaolong/Desktop/daren/30s.mp4):')
        # self.image = 'C:/Users/shixiaolong/Desktop/daren/timg.jpeg'
        # self.image = input('请输入封面图的完整路径比如(C:/Users/shixiaolong/Desktop/daren/timg.jpeg):')
        time.sleep(2)
        # 上传5s视频
        length = 5
        self.five_thirty(self.upload_url,video_5s,length)
        # 上传30s
        driver.get(self.upload_url)
        driver.implicitly_wait(10)
        length = 10
        self.five_thirty(self.upload_url,video_30s,length)
        driver.implicitly_wait(10)
        time.sleep(10)

        driver.get('https://we.taobao.com/material/mine/video?type=video&showDialog=undefined&interactType=chooseVideo')
        driver.find_element_by_xpath('//*[@id="ice_container"]/div/div[2]/div[2]/div/div[3]/div[1]/div[1]/div/div/div/div[2]/div[3]/a').click()
        time.sleep(2)
        # 定位当前窗口
        driver.switch_to_window(driver.window_handles[1])
        driver.find_element_by_xpath('//p[contains(./text(),"边看边买")]').click()
        time.sleep(5)
        # 定位弹窗
        driver.current_window_handle
        try:
            driver.find_element_by_xpath('//div[contains(./text(),"添加宝贝")]').click()
        except:
            driver.find_element_by_xpath('//div[contains(./text(),"添加宝贝")]/parent::div[1]').click()
        time.sleep(5)
        driver.find_element_by_xpath('//div[@role="tab"][2]').click()
        time.sleep(2)
        driver.find_element_by_xpath('//input[@placeholder="请输入宝贝链接"]').send_keys(goods_url)
        driver.find_element_by_xpath('//button[contains(./text(),"添加宝贝")]').click()
        time.sleep(3)
        driver.find_element_by_xpath('//div[@class="creator-add-item-dialog__operation"]/button[contains(./text(),"确定")]').click()
        time.sleep(3)
        driver.current_window_handle
        # 此处默认5张主图
        try:
            driver.find_element_by_xpath('//div[@class="ice-box-wrap"][4]/div/div[1]').click()
        except:
            try:
                driver.find_element_by_xpath('//div[@class="ice-box-wrap"][3]/div/div[1]').click()
            except:
                driver.find_element_by_xpath('//div[@class="ice-box-wrap"][2]/div/div[1]').click()

        print('默认选择第五张主图')
        driver.find_element_by_xpath('//*[@class="next-dialog-footer"]/span/button[1]').click()
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="container"]/div/div[2]/div[2]/div/button[2]').click()
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="container"]/div/div[7]/div/div[2]/div/div/button[2]').click()
        time.sleep(2)
        # 上个步骤完成后,窗口会自动关闭,此时需要回到上一个窗口
        driver.switch_to_window(driver.window_handles[0]) #回到上个窗口
        # 添加已经制作号的互动视频
        driver.get(self.index_page)
        driver.find_element_by_xpath('//*[@id="ice_container"]/div/div[2]/div/div/div[4]/div[1]/div[2]/form/div/div[1]/div/div[1]/div/div').click()
        # 处理弹窗添加互动视频,添加主图
        self.choose_video(image,1)
        time.sleep(3)
        # 添加文案标题和标签
        driver.find_element_by_xpath('//*[@id="title"]').send_keys(self.title)
        self.detail = self.get_detail()
        driver.find_element_by_xpath('//*[@id="summary"]').send_keys(self.detail)
        for i in self.tag:
            driver.find_element_by_xpath('//*[@id="iceAddTagInput"]').send_keys(i)
            driver.find_element_by_xpath('//*[@id="iceAddTagInput"]').send_keys(Keys.ENTER)
        print('导购商品添加完毕')

        # 添加已经上传的5s视频
        # 滚动到目标位置
        command = driver.find_element_by_xpath('//label[@for="homePageIntroVideo"]/following::div[1]/div/div/div')
        driver.execute_script("arguments[0].scrollIntoView();", command)
        command.click()
        self.choose_video(image,2)
        # 分类复选款,并且保存草稿
        self.goods_type(field,people,times)

    # 封装5s和30s视频上传部分
    def five_thirty(self,url,path,length):
        driver.find_element_by_xpath('//input').send_keys(path)
        while True:
            try:
                driver.find_element_by_xpath('//img')
                break
            except:
                print('正在上传,请稍等')
                time.sleep(5)
        time.sleep(length)
        driver.current_window_handle
        driver.find_element_by_xpath('//button[contains(./text(),"完成")]')

    # 添加已经上传视频,上传封面图
    def choose_video(self,image,num):
        driver.current_window_handle
        driver.find_element_by_xpath('//div[{}]/div/div[@class="ice-box-trigger"]'.format(num)).click()
        driver.find_element_by_xpath('//button[contains(./text(),"确定添加")]').click()
        print('主视频编辑完成')
        time.sleep(3)
        # 添加视频封面
        driver.current_window_handle
        driver.find_element_by_xpath('//div[@class="ice-add-image_list-item ice-add-image__uploadbtn"]').click()
        time.sleep(3)
        # 再次打开新的弹窗,选择新上传
        driver.current_window_handle
        driver.find_element_by_xpath('//div[contains(./text(),"上传新图片")]').click()
        element = driver.find_element_by_xpath('//input[@type="file"]')
        driver.execute_script("arguments[0].style.display = 'block';", element)
        time.sleep(3)
        driver.find_element_by_xpath('//input[@type="file"]').send_keys(image)
        time.sleep(3)
        driver.find_element_by_xpath('//button[contains(./text(),"确定")]').click()
        driver.current_window_handle
        time.sleep(3)
        driver.find_element_by_xpath('//button[contains(./text(),"确定")]').click()
        driver.implicitly_wait(10)
        time.sleep(15)
        while True:
            try:
                driver.find_element_by_xpath('//button[contains(./text(),"完成")]').click()
                break
            except Exception as e:
                print(e)
    # 分类复选框
    def goods_type(self,field,people,times):
        # 移动到复选框
        time.sleep(1)
        driver.find_element_by_xpath('//div[@role="tab"][1]').click()
        driver.find_element_by_xpath('//span[contains(./text(),"{}")]/preceding-sibling::span[1]/input'.format(times)).click()
        command = driver.find_element_by_xpath('//div[@role="tab"][2]')
        driver.execute_script("arguments[0].scrollIntoView();", command)
        command.click()
        driver.find_element_by_xpath('//span[contains(./text(),"{}")]/preceding-sibling::span[1]/input'.format(field)).click()
        driver.find_element_by_xpath('//div[@role="tab"][3]').click()
        driver.find_element_by_xpath('//span[contains(./text(),"{}")]/preceding-sibling::span[1]/input'.format('单品展示')).click()
        driver.find_element_by_xpath('//div[@role="tab"][4]').click()
        driver.find_element_by_xpath('//span[contains(./text(),"{}")]/preceding-sibling::span[1]/input'.format(people)).click()
        # 保存草稿
        time.sleep(1)
        driver.find_element_by_xpath('//div[@class="publish-actions"]/button[contains(./text(),"保存草稿")]').click()
        print('草稿保存成功')
        time.sleep(4)

    def main(self):
        while True:
            # 生产
            # self.get_detailinfo()
            jinhao = self.get_jinhao()
            if jinhao != 'no':
                tags = jinhao['tags']

                self.detail_path = jinhao['path']
                field = tags['领域']
                people = tags['人群']
                times = tags['匹配时间']
                with open(self.detail_path + '/info.txt') as f:
                    content = f.read()
                    name_url = json.loads(content)
                goods_url = name_url['url']
                self.goods_title = name_url['name']
                video_5s = self.detail_path + '/' + '5s.mp4'
                video_30s = self.detail_path + '/' + '30s.mp4'
                image = self.detail_path + '/' + '30s.jpg'

                # 消费
                self.upload(video_5s,video_30s,image,goods_url,field,people,times)
                # 清理
                shutil.rmtree(self.detail_path)
            else:
                print('没有可上传的文件,请下载.......')
                time.sleep(60)



if __name__ == '__main__':
    qie = QIE()
    qie.main()

你可能感兴趣的:(自动上传淘宝达人视频)