美文网首页
解放双手:youtube更新的视频自动更新到腾讯平台(增量更新)

解放双手:youtube更新的视频自动更新到腾讯平台(增量更新)

作者: sexy_cyber | 来源:发表于2018-07-11 19:52 被阅读138次
    成功的喜悦

    刚刚测试整个流程走通,已经兴奋的不得了,纯手工打造,绝对原创;

    由于是第一版,功能还不够完善,后面会持续更新......

    from pytube import YouTube
    import requests
    from fake_useragent import UserAgent
    import re
    from selenium import webdriver
    import ssl
    from lxml import etree
    import time
    from selenium.webdriver.common.keys import Keys
    
    
    opt = webdriver.ChromeOptions()
    # opt.add_argument('--headless')
    #更换头部
    cookie = [{'name': 'pt2gguin', 'value': 'o0654921690'}, {'name': 'RK', 'value': 'mSglPLOFQ8'}, {'name': 'ptcz', 'value': '5eb1aeb628b6ab67aa306285a78434959b385f8e39415bda4ff4db3ea7763d75'}, {'name': 'pgv_pvid', 'value': '116706325'}]
    
    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('cookie=%s'% cookie)
    driver = webdriver.Chrome(chrome_options=opt)
    class QIE:
        def __init__(self):
            self.ua = UserAgent()
    
        # 拿到目标视频的链接
        def get_video_urls(self):
    
            index_page = 'https://www.youtube.com/channel/UC6XKMuLChaxW4Isw0BTZI8A/videos'
            headers = {
                'User-Agent':self.ua.random
            }
            proxies = {
                'https':'127.0.0.1:1087'
            }
            list_urls = requests.get(index_page,headers=headers,proxies=proxies).text
            list_urls = re.findall(r'"(/watch\?v=.+?)"',list_urls)
            if len(list_urls)>0:
                list_urls = set(list_urls)
                list_urls = list(list_urls)
                print(list_urls)
    
                if len(list_urls)>5:
                    list_urls = list_urls[:5]
                return list_urls
            else:
                print('匹配失败')
                return 'no'
        # 下载所有需要的视频
        def download_vides(self):
            result = self.get_video_urls()
            if result != 'no':
                self.names = []
                num = 0
                for url in result:
                    print('一共{}个视频'.format(len(result)))
                    ssl._create_default_https_context = ssl._create_unverified_context
                    url = 'https://www.youtube.com' + url
                    yt = YouTube(url=url,proxies={'https':'127.0.0.1:1087'})
                    data = yt.streams.first()
                    file_name = data.default_filename
                    print('视频开始下载......')
                    data.download()
                    num += 1
                    print('第%d个视频下载完成,文件名是:%s' % (num,file_name))
                    self.names.append(file_name)
                return 'ok'
            else:
                return 'no'
    
        # 上传所有本地的视频,每日最多3条
        def upload(self):
            # 请求目标地址
            url = 'https://om.qq.com/article/articlePublish#/!/view:article?typeName=multivideos'
            driver.get(url)
            # 给目标地址添加cookie
            driver.delete_all_cookies()
            for c in cookie:
                new = dict(c,**{
                "domain": ".qq.com",
                "expires": "",
                'path': '/',
                'httpOnly': False,
                'HostOnly': False,
                'Secure': False,
            })
                driver.add_cookie(new)
            # 再次请求目标地址
            driver.get(url)
            # 将本地文件上传到页面的input标签
            path1 = '/Users/admin/Documents/qiehao/'
            file_name = "旅行馬來西亞檳城体验彩虹空中玻璃橋.mp4"
            # 标题名需要小于30个字符
            file_name = file_name.split(' ')[0]
            file_name = path1+file_name
            driver.find_element_by_xpath('//input').send_keys(file_name)
            path = '进入视频上传主页.png'
            driver.save_screenshot(path)
            print('进入视频上传主页截图保存成功')
    
            status = driver.find_element_by_xpath('//span[@class="text-title"]').get_attribute('textContent').strip()
            print(status)
            # 等待上传完成
            num = 0
            while True:
                time.sleep(60)
                num += 1
                status = driver.find_element_by_xpath('//span[@class="text-title"]').get_attribute('textContent').strip()
                path = '载入进度.png'
                driver.save_screenshot(path)
                print('已经上传了第%d分钟'%num)
                if status == '共1个视频,已上传1个':
                    print('ok')
                    break
            path = 'success.png'
            driver.save_screenshot(path)
            print('文件导入成功')
            # 选择分类
            driver.find_element_by_xpath('//span[contains(./text(),"请选择分类")]').click()
            time.sleep(1)
            # 输入内容并且回车
            driver.find_element_by_xpath('//div[@class="chosen-search"]/input').send_keys('少儿节目')
            driver.find_element_by_xpath('//div[@class="chosen-search"]/input').send_keys(Keys.ENTER)
            # 将页面滚动到底部
            js = "var q=document.documentElement.scrollTop=10000"
            driver.execute_script(js)
            time.sleep(3)
    
            # 选择标签,默认选择9个标签
            self.get_tag()
            # 选择封面图
            driver.find_element_by_xpath('//button[contains(./text(),"设置封面")]').click()
            # 滚动到目标位置并且点击
            while True:
                time.sleep(60)
                try:
                    command = driver.find_element_by_xpath('//div[@class="upload-cover-block"]/span/img[contains(./@src,"1.jpg")]')
                    print('图片加载好了')
                    driver.execute_script("arguments[0].scrollIntoView();",command)
                    command.click()
                    break
                except:
                    print('图片还没加载好,继续等待')
            driver.find_element_by_xpath('//button[contains(./text(),"确定")]').click()
            path = '封面设置成功.png'
            driver.save_screenshot(path)
            # 发布
            time.sleep(3)
            while True:
                try:
                    driver.find_element_by_xpath('//button[@action="publish"]').click()
                    path = '上床成功.png'
                    driver.save_screenshot(path)
                    print('上传成功')
                    break
                except:
                    print('设置封面图窗口关闭中')
                    time.sleep(5)
    
    
        def get_tag(self):
            single = driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[1]/a').get_attribute('textContent').strip()
            print(single)
            if single != '暂无':
    
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[1]/a').click()
                except:
                    print('没有推荐的标签')
                    driver.find_element_by_xpath('//label[@class="input-control"]/input[contains(./@id,"videoTags")]').send_keys('玩具')
                    driver.find_element_by_xpath('//label[@class="input-control"]/input[contains(./@id,"videoTags")]').send_keys(Keys.ENTER)
                    # driver.find_element_by_xpath('//label[contains(./text(),"标签")]').click()
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[2]/a').click()
                except:
                    print('没有第二个推荐的标签')
                    driver.find_element_by_xpath('//label[@class="input-control"]/input[contains(./@id,"videoTags")]').send_keys('欧美流行')
                    driver.find_element_by_xpath('//label[@class="input-control"]/input[contains(./@id,"videoTags")]').send_keys(Keys.ENTER)
                    # driver.find_element_by_xpath('//label[contains(./text(),"标签")]').click()
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[3]/a').click()
                except:
                    print('没有第三个推荐的biaoqian ')
    
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[4]/a').click()
                except:
                    print('没有第四个推荐的标签')
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[5]/a').click()
                except:
                    print('没有第五个推荐的标签')
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[6]/a').click()
                except:
                    print('没有第六个推荐的标签')
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[7]/a').click()
                except:
                    print('没有第七个推荐的标签')
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[8]/a').click()
                except:
                    print('没有第八个推荐的标签')
                try:
                    driver.find_element_by_xpath('//li[contains(./text(),"推荐标签")]/following::li[9]/a').click()
                except:
                    print('没有第九个推荐的标签')
            else:
                # 手动输入标签
                driver.find_element_by_xpath('//label[@class="input-control"]/input[contains(./@id,"videoTags")]').send_keys('玩具')
                driver.find_element_by_xpath('//label[@class="input-control"]/input[contains(./@id,"videoTags")]').send_keys(Keys.ENTER)
    
                driver.find_element_by_xpath('//label[@class="input-control"]/input[contains(./@id,"videoTags")]').send_keys('欧美流行')
                driver.find_element_by_xpath('//label[@class="input-control"]/input[contains(./@id,"videoTags")]').send_keys(Keys.ENTER)
    
    
    
    
    
    if __name__ == '__main__':
        qie = QIE()
        qie.upload()
    
    

    视频地址
    http://kuaibao.qq.com/s/20180711V1DJO100

    相关文章

      网友评论

          本文标题:解放双手:youtube更新的视频自动更新到腾讯平台(增量更新)

          本文链接:https://www.haomeiwen.com/subject/qofkpftx.html