大蟒蛇python教程共享Python+selenium实现趣头条的视频自动上传与发布

目录
  • 效果展示
    • 效果展示
    • 素材展示
  • 实现过程
    • 调用已启用的浏览器
    • 上传视频和封面
    • 完整源码展示 
  • 补充
    • python+selenium操作已启用的chrome浏览器
    • python+selenium实现自动导入、上传外部文件

效果展示

效果展示

Python+selenium实现趣头条的视频自动上传与发布

素材展示

一个为视频,另一个为像素大小不小于视频的封面。

Python+selenium实现趣头条的视频自动上传与发布

实现过程

调用已启用的浏览器

通过调用已启用的浏览器,可以实现直接跳过每次的登录过程。

使用方法可以参考补充内容

  from selenium import webdriver    options = webdriver.chromeoptions()  options.add_experimental_option("debuggeraddress", "127.0.0.1:5003")  driver = webdriver.chrome(options = options)  

上传视频和封面

上传功能的使用方法可以参考补充内容

  # 上传视频  driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_mp4)    # 等待视频上传完成  while true:      time.sleep(3)      try:          driver.find_element_by_xpath('//*[contains(text(),"上传成功")]')          break;      except exception as e:          print("视频还在上传中···")    print("视频已上传完成!")    # 添加封面  time.sleep(1)  driver.find_element_by_xpath('//*[@class="el-upload"]').click()  time.sleep(1)  driver.find_element_by_xpath('//*[text()="自定义封面"]').click()  time.sleep(1)  driver.find_element_by_xpath('//*[text()="选择图片"]/../..//input[@type="file"]').send_keys(path_cover)  time.sleep(3)  driver.find_element_by_xpath('//*[text()="确 定"]').click()  

完整源码展示 

  import selenium  from selenium import webdriver  import pathlib  import time  from selenium.webdriver.common.keys import keys    # 基本信息  # 视频存放路径  catalog_mp4 = r"c:usersadministratordesktop视频发布"  # 视频描述  describe = "裸眼3d看蜘蛛侠 #搞笑 #电影 #视觉震撼"  time.sleep(10)  options = webdriver.chromeoptions()  options.add_experimental_option("debuggeraddress", "127.0.0.1:5003")  driver = webdriver.chrome(options = options)    path = pathlib.path(catalog_mp4)    # 视频地址获取  path_mp4 = ""  for i in path.iterdir():      if(".mp4" in str(i)):          path_mp4 = str(i);          break;    if(path_mp4 != ""):      print("检查到视频路径:" + path_mp4)  else:      print("未检查到视频路径,程序终止!")      exit()    # 封面地址获取  path_cover = ""  for i in path.iterdir():      if(".png" in str(i) or ".jpg" in str(i)):          path_cover = str(i);          break;    if(path_cover != ""):      print("检查到封面路径:" + path_cover)  else:      print("未检查到封面路径,程序终止!")      exit()        def publish_qutoutiao():      '''       作用:发布趣头条视频      '''            # 进入创作者页面,并上传视频      driver.get("https://mp.qutoutiao.net/publish-content/video")      time.sleep(2)      driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_mp4)            # 等待视频上传完成      while true:          time.sleep(3)          try:              driver.find_element_by_xpath('//*[contains(text(),"上传成功")]')              break;          except exception as e:              print("视频还在上传中···")            print("视频已上传完成!")            # 输入标题      driver.find_element_by_xpath('//*[@placeholder="内容标题5-30字"]').clear()      time.sleep(2)      driver.find_element_by_xpath('//*[@placeholder="内容标题5-30字"]').send_keys(describe)            # 输入描述信息      time.sleep(1)      driver.find_element_by_xpath('//textarea').clear()      time.sleep(2)      driver.find_element_by_xpath('//textarea').send_keys(describe)            # 选择分类      time.sleep(1)      driver.find_element_by_xpath('//*[@placeholder="请选择分类"]').click()      time.sleep(1)      driver.find_element_by_xpath('//*[text()="电影"]').click()      time.sleep(1)      driver.find_element_by_xpath('//*[text()="分类:"]').click()      time.sleep(1)            # 输入标签      time.sleep(1)      driver.find_element_by_xpath('//*[@class="content-tag"]//input').click()      time.sleep(2)      driver.find_element_by_xpath('//*[@class="content-tag"]//input').send_keys("视觉震撼")      time.sleep(2)      driver.find_element_by_xpath('//*[@class="content-tag"]//input').send_keys(keys.enter)      time.sleep(2)      driver.find_element_by_xpath('//*[@class="content-tag"]//input').send_keys("搞笑")      time.sleep(2)      driver.find_element_by_xpath('//*[@class="content-tag"]//input').send_keys(keys.enter)      time.sleep(2)      driver.find_element_by_xpath('//*[@class="content-tag"]//input').send_keys("电影")      time.sleep(2)      driver.find_element_by_xpath('//*[@class="content-tag"]//input').send_keys(keys.enter)        # 添加封面      time.sleep(1)      driver.find_element_by_xpath('//*[@class="el-upload"]').click()      time.sleep(1)      driver.find_element_by_xpath('//*[text()="自定义封面"]').click()      time.sleep(1)      driver.find_element_by_xpath('//*[text()="选择图片"]/../..//input[@type="file"]').send_keys(path_cover)      time.sleep(3)      driver.find_element_by_xpath('//*[text()="确 定"]').click()                  # 人工进行检查并发布      # time.sleep(3)      # # 点击发布      # driver.find_element_by_xpath('//*[text()="发布"]').click()    # 开始执行视频发布  publish_qutoutiao()  

补充

python+selenium操作已启用的chrome浏览器

首先在 chrome 快捷方式的目标后面加上这个参数。

前面是代表调试端口,可以随便用端口,后面指向一个新的文件夹用于存储用户数据。

注: 后面的参数如果不加上,端口启用好像会失败,目前没有找到原因。

   --remote-debugging-port=5003 --user-data-dir="c:py_seleniumauto"  

Python+selenium实现趣头条的视频自动上传与发布

然后双击快捷快捷方式打开浏览器就好了,现在浏览器就能被控制了。

Python+selenium实现趣头条的视频自动上传与发布

不确定端口是否启用,可以在 cmd 用下面的方法查看端口启用情况。

  c:usersadministrator>netstat -ano | findstr 5003    tcp    127.0.0.1:5003         127.0.0.1:53546        fin_wait_2      12180    tcp    127.0.0.1:5003         127.0.0.1:53547        time_wait       0    tcp    127.0.0.1:5003         127.0.0.1:53858        fin_wait_2      12180    tcp    127.0.0.1:5003         127.0.0.1:53859        time_wait       0    tcp    127.0.0.1:5003         127.0.0.1:53900        fin_wait_2      12180    tcp    127.0.0.1:5003         127.0.0.1:53901        time_wait       0    tcp    127.0.0.1:53546        127.0.0.1:5003         close_wait      15540    tcp    127.0.0.1:53858        127.0.0.1:5003         close_wait      9108    tcp    127.0.0.1:53900        127.0.0.1:5003         close_wait      16320  

通过访问调试端口控制已启用的浏览器。

  from selenium import webdriver    # 另一个导入chrome参数的方法  # from selenium.webdriver.chrome.options import options  # options = options()    options = webdriver.chromeoptions()  options.add_experimental_option("debuggeraddress", "127.0.0.1:5003")  driver = webdriver.chrome(options = options)  print(driver.title)  

运行效果图:

Python+selenium实现趣头条的视频自动上传与发布

python+selenium实现自动导入、上传外部文件

一开始我觉得导入外部文件是需要操作 windows 的文件窗口呢,后来发现原来不用那么麻烦,只要给文件上传的元素传一个本地路径就好了,就是一个 input 类型的。

Python+selenium实现趣头条的视频自动上传与发布

右键元素点击检查可以看到对应的元素。

Python+selenium实现趣头条的视频自动上传与发布

然后 send_keys() 传入路径就好了。

  driver.find_element_by_xpath('//input[@type="file"]').send_keys('c:\users\administrator\desktop\文件.zip') 

以上就是python+selenium实现趣头条的视频自动上传与发布的详细内容,更多关于python selenium视频自动上传与发布的资料请关注<计算机技术网(www.ctvol.com)!!>其它相关文章!

需要了解更多python教程分享Python+selenium实现趣头条的视频自动上传与发布,都可以关注python教程分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/pythontutorial/996496.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐