前言

大家好,我是最渣的黑客,这几天在写selenium web 自动化。如果一个UP主在做自媒体,剪辑好一个视频要上传多个平台,而这些平台还没有互相关联可以进行同步,这个时候上传视频的工作就成了一个繁琐而重复的工作。目前已知的 自媒体平台有很多,我这里就举例比较出名的自媒体平台,百家号,企鹅号,大鱼号,A站,4个平台。重要的代码我会拿出来讲解,文章内容对一些做web自动化的新手比较有用

自动化前的需要准备的 一些 def 代码,来辅助自动化程序:

1.代码功能:上传视频

#encoding:utf-8
"""
功能:文件上传到web
文件名:uploadfile.py
"""
from pywinauto import Desktop
from pywinauto.keyboard import send_keys
import time
def fileFirst(filepath,file):app = Desktop()  # 创建操作桌面的对象dlg = app["打开"]  # 获取弹窗的窗口标题dlg["地址: xiaominToolbar"].click()send_keys(r"%s"%filepath)send_keys("{VK_RETURN}")time.sleep(1)dlg["文件名(&N):Edit"].type_keys("%s"%file)dlg["打开(&O)"].click()def fileNext(filepath,file):app = Desktop()  # 创建操作桌面的对象dlg = app["打开"]  # 获取弹窗的窗口标题dlg["Toolbar3"].click()send_keys(r"%s"%filepath)send_keys("{VK_RETURN}")time.sleep(1)dlg["文件名(&N):Edit"].type_keys("%s"%file)dlg["打开(&O)"].click()if __name__ == '__main__':pass

上述代码,是被主要准备自媒体自动上传视频调用的。

百家号篇:

在我们操作任何一个网站的时候,都需要 登录一个的操作,对我来说百家号的登录是我目前为止遇到最烦人的。因为我们打开百家号登录页面,它会不断的加载网页。而我们的selenium 操作的时候,默认是需要等待网页全部加载完毕,才能进行下一步操作。如果按照正常的操作的话,我们的代码是无法再继续下去。
此时,我们可以看下百家号到底是在加载什么,按F12,找到 newwork ,刷新页面。此时network会下载所有的加载元素,图片等。然后再继续在 Status 那一列中显示是(pending:等待),就是一直在加载的元素。


然后我们鼠标单击上去,查看它的 Headers ,也未发现没有status 请求完成状态。

解决思路:
我们需要把这个域名禁止加载,复制 requests url 的域名:passport.baidu.com,在程序开头写入如下代码:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--host-resolver-rules=MAP passport.baidu.com 127.0.0.1') #禁止加载 passport.baidu.com 域名
driver = webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe", chrome_options=chrome_options)

解决这个问题后,我们还有一个问题,就是百家号,在机器登陆的时候,百度会检测到,时不时会出现旋转图片验证。 我们就需要使用一个方法,cookie 免密登录。

首先在我们需要获取cookie,代码如下:

#encoding:utf-8
from selenium import webdriver
import json
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#capa = DesiredCapabilities.CHROME
#capa["pageLoadStrategy"] = "none" #懒加载模式,不等待页面加载完毕
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--host-resolver-rules=MAP passport.baidu.com 127.0.0.1')
driver = webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe",chrome_options=chrome_options)
baijiahao_login_url = "https://baijiahao.baidu.com/builder/theme/bjh/login"def requcookie():# 百家号登录driver.set_page_load_timeout(5)driver.set_script_timeout(5)try:driver.get(baijiahao_login_url)except:driver.execute_script('window.stop()')time.sleep(10)driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_4__userName"]').click()driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_4__userName"]').send_keys('xxxxxxx')driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_4__password"]').send_keys('xxxxxxx')driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_4__submit"]').click()while True:try:time.sleep(2)home = driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/div[1]/aside/ul/li[1]/div/a').textprint(home)if "首页" == home:print('登录成功')breakexcept:passtime.sleep(10)cookies = driver.get_cookies()f = open('cookie.txt','w')f.write(json.dumps(cookies))f.close()print(cookies)def relogin():# 测试验证免密登录是否成功driver.get(baijiahao_login_url)time.sleep(2)f = open(r'D:\pro_py\auto_office\medio_aotu\cookies_file\cookie.txt','r')cookies = json.loads(f.read())print(cookies)for cookie in cookies:print(cookie)driver.add_cookie(cookie)driver.refresh()if __name__ == '__main__':# requcookie()relogin()

获取到 cookie 后,然后开始正式的写 百家号 web 自动化上传视频的程序了

百家号自动化上传视频 源码如下:

# encoding:utf-8
"""
自媒体号:百家号
编辑作者:技术宅的宋哈哈
python版本:python3.6
登录方式:cookie 免登陆"""from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from uploadfile import fileNext # 调用了准备的代码
from uploadfile import fileFirst # 调用了准备的代码
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import jsondef Baijiahao(video_path, video_file, video_title, img_name,tags):while True:tag = tags  # 标签capa = DesiredCapabilities.CHROMEcapa["pageLoadStrategy"] = "none"  # 懒加载模式,不等待页面加载完毕chrome_options = webdriver.ChromeOptions()chrome_options.add_argument('--host-resolver-rules=MAP passport.baidu.com 127.0.0.1')# driver = webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe", desired_capabilities=capa,chrome_options=chrome_options)driver = webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe", chrome_options=chrome_options)baijiahao_login_url = "https://baijiahao.baidu.com/builder/theme/bjh/login"driver.get(baijiahao_login_url)try:time.sleep(2)f = open(r'D:\pro_py\auto_office\medio_aotu\cookies_file\cookie.txt','r')cookies = json.loads(f.read())for cookie in cookies:driver.add_cookie(cookie)driver.refresh()while True:try:logintext = driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div[1]/div[1]/div/div[2]').textif "布局" in logintext:print('登陆成功')time.sleep(2)breakelse:time.sleep(5)print("登陆中,请稍后")except:passtime.sleep(1)driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div[1]/button/span').click()time.sleep(2)driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/div[1]/aside/div/a').click()time.sleep(5)driver.implicitly_wait(10)driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[1]/div/div/div[1]/div/div[4]/p').click()release = driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[2]/input')driver.execute_script("$(arguments[0]).click()", release)fileFirst(video_path, video_file)while True:try:sp = driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[3]/form/div[4]/div[1]/div[2]/div/div[2]/div[2]').textif "创作大脑为您获取了以上推荐封面" in sp:time.sleep(10)print("上传成功")breakexcept:time.sleep(60)print("正在上传视频中。")passActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[3]/form/div[4]/div[1]/div[2]/div/div[1]/div[1]/div/div/div/div/div[1]/div/div')).perform()replace = driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[3]/form/div[4]/div[1]/div[2]/div/div[1]/div[1]/div/div/div/div/div[1]/div/div/div[2]/div[2]')driver.execute_script("$(arguments[0]).click()", replace)time.sleep(2)while True:try:sctext = driver.find_element_by_xpath('/html/body/div[8]/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div/div/div/div/div/div/span/div/span/div/div/p').textif "本地上传" == sctext:breakexcept:passdriver.find_element_by_xpath('/html/body/div[8]/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div/div/div/div/div/div/span/div/span/div').click()fileNext(video_path, str(img_name))time.sleep(2)driver.implicitly_wait(60)driver.find_element_by_xpath('/html/body/div[8]/div/div[2]/div/div[1]/div[2]/div/button[2]').click() #确认封面 # 当网络错误时候,容易报错。# 标题title = driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[3]/form/div[3]/div[10]/div/div/div/div[1]/div/div[1]/textarea')driver.execute_script("$(arguments[0]).click()", title)driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[3]/form/div[3]/div[10]/div/div/div/div[1]/div/div[1]/textarea').send_keys(Keys.CONTROL, 'a')driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[3]/form/div[3]/div[10]/div/div/div/div[1]/div/div[1]/textarea').send_keys(video_title)# 标签for t in tag:driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[3]/form/div[4]/div[3]/div[2]/div/div/div[1]/input').send_keys('%s'%t)driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[3]/div/div/div[2]/div[3]/div/div/div[3]/form/div[4]/div[3]/div[2]/div/div/div[1]/input').send_keys(Keys.ENTER)# 视频简介keytext = driver.find_element_by_xpath('//*[@id="desc"]')driver.execute_script("$(arguments[0]).click()", keytext)driver.find_element_by_xpath('//*[@id="desc"]').send_keys(Keys.CONTROL, 'a')driver.find_element_by_xpath('//*[@id="desc"]').send_keys(video_title)time.sleep(2)driver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div/div/div[4]/span/button[1]').click()time.sleep(5)driver.close()breakexcept:driver.close()print("程序出错,重新运行")passif __name__ == '__main__':pass

百家号篇,经验总结 :这个过程,我也学习到了很多。上述代码,我主要用到了一个禁止域名的功能,还用到了同伙 while ,判断是否登录或者上传成功。在百家号篇中,点击一些需要使用 driver.execute_script("$(arguments[0]).click()", title) 和 ActionChains(driver).move_to_element(driver.find_element_by_xpath(xxxxx)。一个JS调用一个是 鼠标悬浮 功能。
提示:直接复制代码,不一定能和我的效果是一样,所有代码仅仅作为 参考。

企鹅号篇:

企鹅号,我直接就是使用账户密码登录,也没有用 免cookie登录。相对而言企鹅号的反反机器人机制就比较松了。里面的代码差不多也 百家号篇 一样。这里重要的点基本没有。
代码如下:

# encoding:utf-8
"""
自媒体号:企鹅号
编辑作者:宋哈哈
python版本:python3.6"""
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from  uploadfile import fileFirst # 文件上传 也是同样调用第一次写的上传 函数
from  uploadfile import fileNext # 文件上传 也是同样调用第一次写的上传 函数def Qehao(video_path, video_file, video_title, img_name,tags):driver = webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe")login_url = "https://om.qq.com/userAuth/index"driver.get(login_url)while True:tag = tags()try:time.sleep(3)driver.find_element_by_xpath('//*[@id="screens"]/div[2]/div[4]/div[2]/div[1]/div/div[1]/div[2]/div[1]/img').click()time.sleep(3)driver.switch_to.frame(driver.find_elements_by_tag_name('iframe')[0])driver.switch_to.frame('ptlogin_iframe')driver.find_element_by_xpath('//*[@id="switcher_plogin"]').click()time.sleep(2)driver.find_element_by_xpath('//*[@id="u"]').send_keys('********')driver.find_element_by_xpath('//*[@id="p"]').send_keys('********')driver.find_element_by_xpath('//*[@id="login_button"]').click()time.sleep(3)driver.find_element_by_xpath('//*[@id="root"]/section/article/aside/ul/li[1]/ul/li[1]/a').click()time.sleep(3)driver.find_element_by_xpath('//*[@id="single-spa-application:@om-mixin/main"]/div/div/nav/ul/li[3]/a').click()driver.implicitly_wait(30)driver.find_element_by_xpath('//*[@id="single-spa-application:@om-mixin/main"]/div/section/div/div/button').click()driver.implicitly_wait(30)driver.find_element_by_xpath('/html/body/div[3]/div[2]/section/div/div/section/div[1]/div/div/div/section/div[1]/button/i').click()#视频上传fileFirst(video_path,video_file)time.sleep(2)driver.find_element_by_xpath('//*[@id="om-mixin-main"]/div[4]/div/div[2]/section/div[3]/button[2]').click()driver.implicitly_wait(30)time.sleep(2)#输入标题driver.find_element_by_xpath('//*[@id="-title"]/div/div/div[1]/div/span').clear()driver.find_element_by_xpath('//*[@id="-title"]/div/div/div[1]/div/span').send_keys(video_title)time.sleep(3)#选择分类ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="-category_id"]/div/div/div[2]/div[2]/span[1]')).click().perform()#输入标签ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="-tag"]/div/div/div[1]/div')).click().perform()for t in tag:ActionChains(driver).move_to_element(driver.find_element_by_css_selector('#-tag > div > div > div.omui-suggestion > div > div')).send_keys('%s'%t).perform()ActionChains(driver).move_to_element(driver.find_element_by_css_selector('#-tag > div > div > div.omui-suggestion > div > div')).send_keys(Keys.ENTER).perform()#输入简介driver.find_element_by_xpath('//*[@id="-desc"]/div/div/textarea').send_keys(video_title)time.sleep(2)driver.find_element_by_xpath('//*[@id="-poster"]/div/div/div[1]/div/button').click()fileNext(video_path,str(img_name))time.sleep(1)while True:endtext = driver.find_element_by_xpath('//*[@id="single-spa-application:@om-mixin/main"]/div/section/div/div/div[2]/div[1]/div/div[1]/div/div/span').textif "成功" in endtext:print("上传成功,马上发布")time.sleep(10)driver.find_element_by_xpath('//*[@id="single-spa-application:@om-mixin/main"]/footer/div/ul/li[1]/button/span').click()time.sleep(5)driver.close()breakelse:time.sleep(60)print("未检测到上传成功,请等待")except:driver.close()print('程序出错,重新运行')if __name__ == '__main__':Qehao()

上述就是全部的企鹅号,自动化上传视频的全部代码了。

大鱼号篇:

在大鱼号,这里我要重点说下。因为大鱼号是 阿里旗下的,所以基本上用的反反机器人策略是和淘宝阿里巴巴这样的网站是一样的。现在唯一能破解的登录,就是 cookie 免密登录,此时肯定有人说,不是滑动下验证码就行了吗。那你可能就大错特错了,不信你可以去试试。

滑动代码如下【这里仅仅滑动代码,并无输入账户密码登录等功能,需要的请复制需要的功能代码】:


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
#chrome_options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe",chrome_options=chrome_options)
#chrome_options.add_argument(
#    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36')
#driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",
#                      {
#                           "source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""", })
dayuhao_login_url = "https://mp.dayu.com/"
span_background = driver.find_element_by_xpath('//*[@id="nc_1__scale_text"]/span')
span_background_size = span_background.size
print(span_background_size)
# 获取滑块的位置
button = driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')
button_location = button.location
print(button_location)
x_location = span_background_size["width"]
y_location = button_location["y"]
print(x_location, y_location)
chrome_options = webdriver.ChromeOptions()# 设置浏览器初始 位置x,y & 宽高x,y
chrome_options.add_argument(f'--window-position={217},{172}')
chrome_options.add_argument(f'--window-size={1200},{1000}')# 关闭自动测试状态显示 // 会导致浏览器报:请停用开发者模式
# window.navigator.webdriver还是返回True,当返回undefined时应该才可行。
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""",
})
action = ActionChains(driver)
source = driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')
action.click_and_hold(source).perform()
action.move_by_offset(246, 0)
action.release().perform()

当然,网上有很多破解 阿里系的滑动验证办法,反正我每个都试过,都没成功。貌似有个成功,但是导致我无法在自动输入账户密码。
第一修改 浏览器驱动的代码程序:
修改 chromedriver.exe 的程序代码。用notepad ++ 管理员运行,运行前需要关闭 chromedriver.exe ,右键任务栏,调出任务管理器,然后在进程哪里全部关闭:


然后用notepad ++ 管理员模式运行,打开 chromedriver.exe ,打开后显示乱码,不要紧,按 crtl + F 搜索 关键词 $cdc_开头的key 。找到后修改这里把 $ cdc_asdjflasutopfhvcZLmcfl_”更改为“$chr_fajfjafjasifjsiaSsdjdl_”。总之随便写就行,就是字符位数要一样。


第二,添加以下代码,看不懂的直接复制就行。


chrome_options = webdriver.ChromeOptions()# 设置浏览器初始 位置x,y & 宽高x,y
chrome_options.add_argument(f'--window-position={217},{172}')
chrome_options.add_argument(f'--window-size={1200},{1000}')# 关闭自动测试状态显示 // 会导致浏览器报:请停用开发者模式# window.navigator.webdriver还是返回True,当返回undefined时应该才可行。
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])# 关闭开发者模式
#chrome_options.add_experimental_option("useAutomationExtension", False)# 禁止图片加载
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)# 设置中文
chrome_options.add_argument('lang=zh_CN.UTF-8')# 更换头部
chrome_options.add_argument('你的浏览器user-agent')# 隐藏浏览器
#chrome_    options.add_argument('--headless') #隐藏浏览器# 部署项目在linux时,其驱动会要求这个参数
chrome_ options.add_argument('--no-sandbox')# 创建浏览器对象
driver = webdriver.Chrome("chromedriver.exe的本地path,根据自己实际填写", options=chrome_options)# 设置执行js代码转换模式
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""",})

差不多按照上面两个办法,貌似可以实现 阿里滑动验证的变成 成功,而不是提示报错提示刷新,但是我在写上去后,账户密码不能自动输入了。可能是我开启了懒加载模式,导致元素不能加载出来,从而报错找不到改元素。是这两串代码:

capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"  # 懒加载模式,不等待页面加载完毕
chrome_options = webdriver.ChromeOptions()
driver =webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe",desired_capabilities=capa,chrome_options=chrome_options)

大鱼号总结经验:自动化这个程序,让我认识到了,个个平台的防机器人的还是阿里比较强一点。但是办法总比困难多。任何登录,都可以用 cookie 免密登录,去解决。

下方是 大鱼号自动上传视频的全部代码:

# encoding:utf-8
"""
自媒体号:大鱼号
编辑作者:宋哈哈
python版本:python3.6
登录方式:cookie 免登陆"""
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from uploadfile import fileFirst  # 文件第一次上传
from uploadfile import fileNext  # 文件第二次上传
import json
def Dayuhao(video_path, video_file, video_title, img_name,tags):while True:tag = tagschrome_options = webdriver.ChromeOptions()chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])chrome_options.add_experimental_option("useAutomationExtension", False)driver = webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe", chrome_options=chrome_options)chrome_options.add_argument('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36')driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{"source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""", })dayuhao_login_url = "https://mp.dayu.com/"try:driver.implicitly_wait(10)driver.delete_all_cookies()driver.get(dayuhao_login_url)f1 = open(r'D:\pro_py\auto_office\medio_aotu\cookies_file\vcyber.json')cookie = f1.read()cookie = json.loads(cookie)for c in cookie:driver.add_cookie(c)# # 刷新页面driver.refresh()time.sleep(5)driver.implicitly_wait(10)# 封面上传driver.find_element_by_xpath('//*[@id="menu"]/ul/li[6]/a').click()time.sleep(1)driver.find_element_by_xpath('//*[@id="w-menu-contents_material"]/span').click()time.sleep(2)# 删除封面try:ActionChains(driver).move_to_element(driver.find_element_by_xpath('/html/body/div/div[3]/div/div[2]/div/div[1]/div[1]/div/i')).click().perform()time.sleep(1)ActionChains(driver).move_to_element(driver.find_element_by_xpath('/html/body/div/div[3]/div/div[2]/div/div[1]/div[1]/button')).click().perform()time.sleep(1)ActionChains(driver).move_to_element(driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div[2]/div/button[2]')).click().perform()except:passtime.sleep(2)ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="imageUpload"]/div[1]/div')).click().perform()fileFirst(video_path,str(img_name))# 切换到视频上传time.sleep(30)driver.find_element_by_xpath('//*[@id="w-menu-"]').click()time.sleep(2)driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/ul/li[2]/a').click()time.sleep(2)driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div/div/div[3]/div[1]/div[1]').click()fileNext(video_path, video_file)time.sleep(5)while True:try:video_end = driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[1]/div[1]/div/div[2]/p[1]').textif "成功" in video_end:print("上传成功,可以发布")time.sleep(3)breakelse:time.sleep(60)print("未检测到上传成功,请等待")except:pass# 输入标题driver.find_element_by_xpath('//*[@id="video_title"]').clear()driver.find_element_by_xpath('//*[@id="video_title"]').send_keys(video_title)time.sleep(1)# 输入视频简介driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[1]/div[3]/div/textarea').clear()driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[1]/div[3]/div/textarea').send_keys(video_title)# 输入标签ActionChains(driver).move_to_element(driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[1]/div[4]/div/div/div[1]')).click().perform()for t in tag:ActionChains(driver).move_to_element(driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[1]/div[4]/div/div/div[1]')).send_keys('%s'%t).perform()ActionChains(driver).move_to_element(driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[1]/div[4]/div/div/div[1]')).send_keys(Keys.ENTER).perform()time.sleep(2)# 选择视频分类driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[1]/div[5]/div/div/div[1]').click()driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[1]/div[5]/div/div/div[2]/a[41]').click()# 设置封面ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="coverImg"]/div[1]/div')).perform()time.sleep(1)driver.find_element_by_xpath('//*[@id="coverImg"]/div[1]/div').click()time.sleep(1)driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div[1]/div/div[1]/div/div[2]/div[1]/div[1]/div/img').click()time.sleep(1)driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div[1]/div/div[1]/div/div[2]/div[1]/div[1]/div').click()time.sleep(1)driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div[1]/div/div[1]/div/div[2]/div[1]/div[1]/div').click()time.sleep(1)driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div[1]/div/div[2]/div/button[2]').click()time.sleep(1)driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div[2]/div[3]/div[2]/button[2]').click()# 上传time.sleep(10)driver.find_element_by_xpath('/html/body/div[1]/div[4]/div/div[2]/div/div/div[2]/div/button[1]').click()time.sleep(2)driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div[5]/div/button').click()time.sleep(5)driver.close()breakexcept:driver.close()print('程序出错,重新运行中')if __name__ == '__main__':dayuhao()

Acfun 站篇:

A站 其实对于 反反机器人机制,做的不是严,甚至有多个账号,还可以进行刷评论,刷播放,刷弹幕 等等一些操作。有些web和程序对一些固定时间操作的,会识别成机器人。破解方法就是 利用random 模块和 time ,把 操作时间 随机化,如果遇到检测IP,也可以使用代理IP等。对于A站的没有任何重点可以将,目前为止正常操作就行。

以下是A站上传视频自动化全部代码:

# encoding:utf-8
"""
自媒体号:ACFUN
编辑作者:宋哈哈
python版本:python3.6
"""from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from uploadfile import fileFirst  # 文件上传
from uploadfile import fileNext  # 文件上传def Acfun(video_path, video_file, video_title, img_name,tags):while True:tag = tags # 标签driver = webdriver.Chrome(r"D:\pro_py\auto_office\chromedriver\chromedriver.exe")login_url = "https://www.acfun.cn/login"driver.get(login_url)try:# 登录账号driver.find_element_by_xpath('//*[@id="login-switch"]').click()time.sleep(1)driver.find_element_by_xpath('//*[@id="ipt-account-login"]').send_keys('15574506809')time.sleep(1)driver.find_element_by_css_selector('#ipt-pwd-login').send_keys('xat12345')time.sleep(1)driver.find_element_by_xpath('//*[@id="form-login"]/div[4]/div').click()# 上传视频阶段time.sleep(10)driver.find_element_by_xpath('//*[@id="header-guide"]/li[7]/a').click()time.sleep(2)driver.switch_to.window(driver.window_handles[1])  # 切换浏览器的新页面标签driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/div[1]/div[3]/div/div/div/div[2]').click()time.sleep(1)fileFirst(video_path, video_file)while True:try:successful = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/div[2]/div/div[2]/span/div/div[1]/span').textprint(successful)if "完成" in successful:print("上传成功")breakelse:time.sleep(60)print("正在上传中,请稍后")except:pass# 标题driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[2]/div[2]/div/div/div/textarea').clear()driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[2]/div[2]/div/div/div/textarea').send_keys(video_title)# 原创类型time.sleep(1)driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[3]/div[2]/div/div/div/label[1]/span[1]/input').click()# 视频分类、time.sleep(2)driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[4]/div[2]/div/div/input').click()time.sleep(1)driver.find_elements_by_class_name('el-cascader-node__label')[5].click()driver.find_elements_by_class_name('video-cascader-des')[0].click()# 视频标签time.sleep(2)for t in tag:driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[5]/div/div/div[2]/div[1]/div[2]/input').send_keys('%s'%t)driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[5]/div/div/div[2]/div[1]/div[2]/input').send_keys(Keys.ENTER)# 视频简介time.sleep(2)driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[6]/div[2]/div/div/div/textarea').send_keys(video_title)time.sleep(2)# 视频封面driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[1]/div[1]/div[1]/div/div/div').click()fileNext(video_path, str(img_name))time.sleep(2)ActionChains(driver).move_to_element(driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div/div[3]/div/button/span')).click().perform()time.sleep(2)# 点击发布time.sleep(10)driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div/div/div/form/div/div[13]/button/span/span').click()time.sleep(5)driver.close()breakexcept:driver.close()print("运行出错,重新运行")if __name__ == '__main__':Acfun()

自媒体自动化全部总结:

可以来说,写出来那几个网站上传视频自动化,那基本上对 selenium 自动化 基础的都了解了。当然还有很多更多的,还需要不断地学习,这里我讲解的还不是很详细。在需要登录多个账号的时候,用 cookie 免密登录,其实是比较费时间的。这个时候就需要破解各种验证码。有滑动验证,滑动旋转图片验证,有点击文字验证,有拼图验证,有数字验证,有识物验证等等,都需要这些破解没有标准答案,都需要自己去好好琢磨,就好像是在做一道数学的解答题一样。

希望这些代码和问题,能给你带来帮助,如果遇到不懂的,请在下方留言,看到第一时间会回复你。还有就是点个关注呗。

python3 selenium web自媒体百家号企鹅号大鱼号acfun站,自动化上传视频以及经验总结分享相关推荐

  1. Python3下Web下载媒体小工具(常用来下B站视频)

    Python3下Web下载媒体小工具(常用来下B站视频) 项目地址:https://github.com/soimort/you-get 使用条件 需要以下依赖项: Python 3.2或以上 FFm ...

  2. 大鱼号自媒体怎么赚钱?怎么通过大鱼号赚钱?

    大鱼号自媒体怎么赚钱?怎么通过大鱼号赚钱? 大鱼号是阿里巴巴旗下的自媒体平台,实现了内容创作一点接入"大鱼号",即可多点分发到UC.UC头条.优酷.土豆.淘宝.神马搜索.豌豆荚.后 ...

  3. 腾讯视频上传视频如何同步到企鹅号

    企鹅号是腾讯的一个非常棒的产品,让一大批自媒体人赚了钱,我们都知道腾讯视频和企鹅号是互通的,腾讯视频中的视频也可以同步到企鹅号中.所以今天播放器家园网小编教大家如何将视频进行同步. 腾讯视频上传视频如 ...

  4. 微信小程序也许会用到上传视频,针对视频转码转为m3U8格式即web端可以使用的解决办法

    媒体处理这部分的视频转码涉及到 阿里云服务的对象存储OSS服务,消息服务MNS,及媒体处理MTS服务 可以利用阿里云服务的MTS服务即媒体处理  https://mts.console.aliyun. ...

  5. web利用腾讯云点播上传视频

    web利用腾讯云点播上传视频到云服务器 第一步导入 <script src="//imgcache.qq.com/open/qcloud/js/vod/sdk/ugcUploader. ...

  6. springmvc web网站开发上传视频到远程服务器解决方案

    springmvc web网站开发上传视频到远程服务器解决方案!近期在给学校做官方网站设计时,有一个业务需求是,后台要增加一个视频管理模块,管理员在后台可以把本地硬盘剪辑好的视频文件,上传到远程服务器 ...

  7. 微信公众号如何上传视频

    第一步:在微信公众号新建图文中,选择右侧的视频: 第二步:新建视频: 第三步:若视频小于20MB,上传即可,否则点击腾讯视频的链接,进入腾讯视频网页版(个人认为小于20MB的视频很小): 第四步:点击 ...

  8. web端利用腾讯云点播接口上传视频

    腾讯云web上传文件文档 https://cloud.tencent.com/document/product/266/9239#.E5.B8.B8.E8.A7.81.E9.97.AE.E9.A2.9 ...

  9. 【甄选靶场】Vulnhub百个项目渗透——项目八:IMF-1(文件上传,缓冲区溢出)

    Vulnhub百个项目渗透 靶场环境 提取码:rong Vulnhub百个项目渗透--项目八:IMF-1(文件上传,缓冲区溢出)

最新文章

  1. 提供呼叫中心服务器,呼叫中心系统方案
  2. html 一个圆圈一个c,如何用c语言程序画一个圆?
  3. Mysql实现幂等_阿里面试官:接口的幂等性怎么设计?
  4. Java中的ASCII码与Unicode码
  5. python与线性代数 矩阵方程
  6. 永中office之在线预览(vue/js版)
  7. 软考软件设计师本人成功备考经验分享
  8. matlab由滤波的系数得到传输函数 设计带通滤波器 design fdatool设计IIR带通滤波器
  9. Field userDao ....service.impl...'com.lzj.springcloud.dao.UserDao' that could not be found
  10. 如何用串口助手测试软件485通讯功能,串口调试助手如何检测RS485端口好坏及信号发送的好坏?...
  11. Spring Boot统一日志框架
  12. 美国大学计算机科学gpa,美国大学MS硕士平均GPA是多少分?
  13. 移动APP卡顿问题解决实践
  14. 小寒也会写程序(一)
  15. Qt给字体设置下划线
  16. C# - Tester-Doer Pattern
  17. svn: OPTIONS of 'https://lym-pc/svn/CRM': Could not resolve hostname 'lym-pc'
  18. PLSql连接Oracle时提示TNS:无监听程序的解决方法
  19. 完全二叉树 和 满二叉树的区别
  20. 韦尔奇经典的一次演讲:2001年夏,在股东年会上的讲话(中英文)

热门文章

  1. 70行python代码实现qq视频加特效效果
  2. R7 5800H和i5 11300H参数对比差距大不大
  3. JS Array.slice 截取数组的实现方法
  4. java求最短距离,Java实现 LeetCode 821 字符的最短距离(暴力)
  5. 李宏毅深度学习笔记(CNN)
  6. 关于CreateParams的参数
  7. MPU9250配置及原始数据读取
  8. 虚函数和纯虚函数及虚函数表
  9. 杭州师范大学c语言程序设计机试,杭州师范大学C语言试题第3套.pdf
  10. 什么是IT咨询?IT外包又是什么?