迅雷号自媒体视频文件自动上传,贴标签发布

难点

本地文件上传,通过send_keys(‘文件路径’)的方式实现上传的目的

文件名通过正则匹配的方式进行处理,主要匹配出中文标题名称

处理过程中文件名称中包括中文字符,特殊字符,数字等

视频文件上传是否完成的判断,视频上传的进度条通过js加载,在上传的过程中让程序进入到循环中进行等待进度条的加载完成,根据加载完成后标签的内容来判定是否上传完成

发布是否成功的判断,发布成功前后页面几乎没有变化,通过查看元素查找上传成功和失败有哪些细微的变化,从细微的变化中找到可以依据判断是否上传成功的条件

import sys

from threading import Thread

from selenium import webdriver

from selenium.webdriver.common.keys import Keys #引入Keys类包

import time

import re, os

from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

from multiprocessing import Queue

from ua import *

path_queue = Queue()

path = r'C:\Users\Administrator\Desktop\video'

run_log = {

'login_status': [],

'publish_status': [],

}

class XunleiPublish(object):

def __init__(self):

self.url = 'http://mp.m.xunlei.com/'

def login(self, id):

self.opt = webdriver.ChromeOptions()

self.opt.set_headless()

# 设置随机请求头

self.opt.add_argument('user-agent=' + getheaders())

self.driver = webdriver.Chrome(chrome_options=self.opt,executable_path='chromedriver.exe')

self.driver.get(self.url)

time.sleep(1)

try:

WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, 'loginIframe')))

# 找到iframe标签

self.driver.switch_to.frame('loginIframe')

self.driver.find_element_by_id('al_u').send_keys(id[:10])

self.driver.find_element_by_id('al_p').send_keys(id[11:])

self.driver.find_element_by_id('al_submit').click()  # 登陆按钮

self.driver.find_element_by_class_name('header_info_name')

run_log['login_status'].append('%s:登陆成功!' % id[:10])

self.driver.get('http://mp.m.xunlei.com/publish')

time.sleep(0.1)

self.upload(id)

except Exception as e:

run_log['login_status'].append('%s:登陆失败!' % id[:10])

self.driver.quit()

sys.exit()

def upload(self, id):

i = 0

while True:

if not path_queue.empty():

filepath = path_queue.get()

while True:

try:

self.driver.find_element_by_xpath('//input[@type="file"]')

break

except:

pass

# 找到input标签把所需要上传的文件发送过去

self.driver.find_element_by_xpath('//input[@type="file"]').send_keys(filepath)

time.sleep(2)

self.publish(filepath)

# 分类标签如果没有分类则发布成功

cate_attr = self.driver.find_element_by_class_name('catetxt')

if cate_attr.text == '精确选择分类更有利于推荐':

i += 1

# print('第%d次发布成功!' % i)

# run_log['publish_status'].append('%s第%d次发布成功!' % (id[:10], i))

run_log['publish_status'].append('{}:第{}次发布成功!'.format(id[:10], i))

os.remove(filepath)

else:

os.remove(filepath)

self.driver.refresh()

if i >= 500:

self.driver.quit()

sys.exit()

else:

break

self.driver.quit()

sys.exit()

def publish(self, filepath):

title_list = re.compile('([^\x00-\xff]+)', re.S).findall(filepath)

while True:

try:

upload_progress = self.driver.find_element_by_id('showTips').find_element_by_tag_name('font').text

if upload_progress == '已为您上传成功!':

break

elif upload_progress == '上传失败!':

os.remove(filepath)

break

except:

pass

time.sleep(2)

try:

if title_list:

self.driver.find_element_by_xpath('//input[@id="title"]').send_keys(','.join(title_list))

self.driver.find_element_by_id('topicInput').send_keys('搞笑')

self.driver.find_element_by_class_name('span_squ').click()

self.driver.find_elements_by_class_name('option_item')[0].click()

# 发送文字到input框按下回车键

self.driver.find_element_by_id('tag-selectized').send_keys('搞笑', Keys.ENTER)

self.driver.find_element_by_id('prot_check').click()  # 点击同意平台服务协议按钮

self.driver.find_element_by_id('publishBtn').click()  # 点击发布按钮

time.sleep(2)

except:

self.driver.refresh()

def file(self):

for filename in os.listdir(path):

filepath = path + '\\' + filename

path_queue.put(filepath)

# if __name__ == '__main__':

#     XunleiPublish().file()

#     thread_list = []

#     with open('account_info.txt', 'r', encoding='utf-8') as f:

#         s = f.readlines()

#         for id in s[21:]:

#             t = Thread(target=XunleiPublish().login, args=(id.replace('\n', ''),))

#             thread_list.append(t)

#             t.start()

#         for i in thread_list:

#             i.join()

# 返回登录日志

def login_record():

while 1:

if run_log['login_status']:

return run_log['login_status']

time.sleep(0.1)

# 返回发布日志

def publish_record():

while 1:

if run_log['publish_status']:

return run_log['publish_status']

time.sleep(0.1)

if __name__ == '__main__':

Pass

pyqt界面

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'

#

# Created by: PyQt5 UI code generator 5.11.3

#

# WARNING! All changes made in this file will be lost!

import ctypes

import win32con

from PyQt5 import QtCore, QtWidgets

from PyQt5.QtCore import QThread, pyqtSignal

from PyQt5.QtWidgets import QApplication, QMainWindow

from thr_publish import *

import sys, time

SESSION_DATA = False

SHOW_S_P = False

class Worker(QThread):

valueChanged = pyqtSignal(int)

handle = -1

def run(self):

global SESSION_DATA, EXIT_COND

try:

self.handle = ctypes.windll.kernel32.OpenThread(

win32con.PROCESS_ALL_ACCESS, False, int(QThread.currentThreadId())

)

except Exception as e:

print('get thread handle failed', e)

# 循环发送信号

while True:

if SESSION_DATA:

self.valueChanged.emit(1024)

SESSION_DATA = False

time.sleep(0.1)

def exi_thread(self):

os._exit(122)

class Ui_MainWindow(QMainWindow):

thread_list = []

def __init__(self):

super(Ui_MainWindow, self).__init__()

self.publish_record_count = 0

for func in [self.output_login_status, self.publish_record]:

thr = Thread(target=func)

thr.setDaemon(True)

thr.start()

# 子线程

self._thread = Worker(self)

self._thread.finished.connect(self._thread.deleteLater)

# self._thread.valueChanged.connect(ex.create_c)

self._thread.start()

def setupUi(self, MainWindow):

MainWindow.setObjectName("MainWindow")

MainWindow.resize(1222, 665)

self.centralwidget = QtWidgets.QWidget(MainWindow)

self.centralwidget.setObjectName("centralwidget")

self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)

self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 0, 911, 591))

self.gridLayoutWidget.setObjectName("gridLayoutWidget")

self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)

self.gridLayout.setContentsMargins(0, 0, 0, 0)

self.gridLayout.setObjectName("gridLayout")

self.tableWidget_2 = QtWidgets.QTableWidget(self.gridLayoutWidget)

self.tableWidget_2.setObjectName("tableWidget_2")

self.tableWidget_2.setColumnCount(4)

self.tableWidget_2.setRowCount(0)

item = QtWidgets.QTableWidgetItem()

self.tableWidget_2.setHorizontalHeaderItem(0, item)

item = QtWidgets.QTableWidgetItem()

self.tableWidget_2.setHorizontalHeaderItem(1, item)

item = QtWidgets.QTableWidgetItem()

self.tableWidget_2.setHorizontalHeaderItem(2, item)

item = QtWidgets.QTableWidgetItem()

self.tableWidget_2.setHorizontalHeaderItem(3, item)

self.gridLayout.addWidget(self.tableWidget_2, 0, 1, 1, 1)

self.listWidget = QtWidgets.QListWidget(self.gridLayoutWidget)

self.listWidget.setObjectName("listWidget")

self.gridLayout.addWidget(self.listWidget, 1, 1, 1, 1)

# 运行日志

self.textBrowser_1 = QtWidgets.QTextBrowser(self.listWidget)

self.textBrowser_1.setGeometry(QtCore.QRect(2, 2, 521, 260))

self.textBrowser_1.setObjectName("textBrowser")

# self.textBrowser_1.append('运行日志')

self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)

self.verticalLayoutWidget.setGeometry(QtCore.QRect(920, 0, 301, 131))

self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")

self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)

self.verticalLayout.setContentsMargins(0, 0, 0, 0)

self.verticalLayout.setObjectName("verticalLayout")

self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)

self.pushButton.setObjectName("pushButton")

self.verticalLayout.addWidget(self.pushButton)

self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)

self.horizontalLayoutWidget.setGeometry(QtCore.QRect(920, 180, 301, 71))

self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")

self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)

self.horizontalLayout.setContentsMargins(0, 0, 0, 0)

self.horizontalLayout.setObjectName("horizontalLayout")

self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)

self.label.setObjectName("label")

self.horizontalLayout.addWidget(self.label)

self.lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget)

self.lineEdit.setObjectName("lineEdit")

self.horizontalLayout.addWidget(self.lineEdit)

self.horizontalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)

self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(920, 280, 301, 80))

self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")

self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)

self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)

self.horizontalLayout_2.setObjectName("horizontalLayout_2")

self.label_2 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)

self.label_2.setObjectName("label_2")

self.horizontalLayout_2.addWidget(self.label_2)

self.lineEdit_2 = QtWidgets.QLineEdit(self.horizontalLayoutWidget_2)

self.lineEdit_2.setObjectName("lineEdit_2")

self.horizontalLayout_2.addWidget(self.lineEdit_2)

MainWindow.setCentralWidget(self.centralwidget)

self.menubar = QtWidgets.QMenuBar(MainWindow)

self.menubar.setGeometry(QtCore.QRect(0, 0, 1222, 26))

self.menubar.setObjectName("menubar")

MainWindow.setMenuBar(self.menubar)

self.statusbar = QtWidgets.QStatusBar(MainWindow)

self.statusbar.setObjectName("statusbar")

MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)

QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):

_translate = QtCore.QCoreApplication.translate

MainWindow.setWindowTitle(_translate("MainWindow", "迅雷号"))

item = self.tableWidget_2.horizontalHeaderItem(0)

item.setText(_translate("MainWindow", "账号"))

item = self.tableWidget_2.horizontalHeaderItem(1)

item.setText(_translate("MainWindow", "密码"))

item = self.tableWidget_2.horizontalHeaderItem(2)

item.setText(_translate("MainWindow", "登陆状态"))

item = self.tableWidget_2.horizontalHeaderItem(3)

item.setText(_translate("MainWindow", "上传数量"))

self.pushButton.setText(_translate("MainWindow", "登陆账号"))

self.pushButton.clicked.connect(self.login)

self.label.setText(_translate("MainWindow", "文件路径:"))

self.label_2.setText(_translate("MainWindow", "添加话题:"))

# 点击登陆

def login(self):

XunleiPublish().file()

self.textBrowser_1.append('开始登陆,上传发布...')

# thread_list = []

with open('account_info.txt', 'r', encoding='utf-8') as f:

s = f.readlines()

for id in s[:5]:

t = Thread(target=XunleiPublish().login, args=(id.replace('\n', ''),))

self.thread_list.append(t)

t.setDaemon(True)

t.start()

# for i in self.thread_list:

#     i.join()

# 日志更新

def output_login_status(self):

# 登陆成功输出

while True:

# 登陆日志

login_record_list = login_record()

if login_record_list:

for i in login_record_list:

self.textBrowser_1.append(i)

self.textBrowser_1.moveCursor(self.textBrowser_1.textCursor().End)

login_record_list.remove(i)

time.sleep(0.1)

# 发布日志

def publish_record(self):

while True:

publish_record_list = publish_record()

if publish_record_list:

for record in publish_record_list:

self.textBrowser_1.append(record)

self.textBrowser_1.moveCursor(self.textBrowser_1.textCursor().End)

publish_record_list.remove(record)

time.sleep(0.1)

if __name__ == '__main__':

app = QApplication(sys.argv)

MainWindow = QMainWindow()

ui = Ui_MainWindow()

ui.setupUi(MainWindow)

MainWindow.show()

sys.exit(app.exec_())

# 打包封装为.exe可执行文件

# pyinstaller -F -w untitled.py -p thr_publish.py -p ua.py --hidden-import untitled --hidden-import thr_publish --hidden-import ua

原文链接:https://blog.csdn.net/qq_41866851/java/article/details/95781660

python+selenium上传本地文件的更多相关文章

  1. robot_framework + selenium + 上传本地文件+win7 32位

    1.下载与安装AutoIt v3  地址链接:http://pan.baidu.com/s/1hqsDFBA,我自己是32位的系统,用这个运行可以 2.安装完成后,如下图所示 3. AutoIt Wi ...

  2. paramiko模块的安装和使用(含上传本地文件或文件夹到服务器,以及下载服务器文件到本地)

    安装和使用分两步介绍: 介绍一下,本文的运行环境是win7 64位 和python 2.7  . 安装: WIN7_64位 安装python-ssh访问模块(paramiko)的安装教程,本人亲测下面 ...

  3. 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器

    引入: 在我们昨天架设好了Samba服务器上并且创建了一个 Samba 账户后,我们就迫不及待的想用JAVA去操作Samba服务器了,我们找到了一个框架叫 jcifs,可以高效的完成我们工作. 实践: ...

  4. git 上传本地文件到github

    git 上传本地文件到github 1 git config --global user.name "Your Real Name" 2 git config --global u ...

  5. 如何用一张图片代替 'input:file' 上传本地文件??

    今天去面试,碰到了一道题,也许是因为紧张或者喝水喝多了,一时竟然没有转过弯来,回来之后一细想原来这么简单,哭笑不得,特此记录一下! 原题是这样的:  如何用一张图片代替 'input:file' 上传 ...

  6. 两种方法上传本地文件到github

    https://www.jianshu.com/p/c70ca3a02087 自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的 ...

  7. Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件

    利用ssh传输文件   在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件 scp username@servername:/path/filename /var/www ...

  8. Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件

    http://blog.csdn.net/rodulf/article/details/71169996 利用ssh传输文件 在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下 ...

  9. 两种方法上传本地文件到github(转)

    自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的原因是我习惯本地编辑,完成以后再一起上传github.看过了几个教程,总结出最 ...

随机推荐

  1. Mac node.js express-generator脚手架安装

    前言 由于本人在学习NodeJs的express框架时,在Mac电脑上安装express遇到了一个深痛的坑点,特写此文来记录.该坑点的解决方案我在国内的度娘没有找到,问别人也没有方案,最后通过goog ...

  2. 【MySQL】MySQL5.7等以上版本在Windows上的配置

    由于本人是win10系统,所以说下win10系统以管理员身份打开cmd 1. 配置环境变量 我这边是安装在了C:\Program Files\MySQL\MySQL Server 5.7在path中加 ...

  3. 4、Servlet中的Cookie 用于存储 web 页面的用户信息。

    Servlet Cookie 处理 Cookie 是存储在客户端计算机上的文本文件,并保留了各种跟踪信息.Java Servlet 显然支持 HTTP Cookie. 识别返回用户包括三个步骤: 服务 ...

  4. 三、HTML元素

    嵌套的HTML元素 <!--以下实例包含了三个HTML元素,分别是<html>.<body>.<p>--> <!DOCTYPE html> ...

  5. Kafka SSL安装与配置

    1.概述 最近有同学咨询说,Kafka的SSL安全认证如何安装与使用?今天笔者将通过以下几个方面来介绍Kafka的SSL: Kafka 权限介绍 Kafka SSL的安装与使用 Kafka Eagle ...

  6. unix 密码破解,zip破解总结

    unix /etc/passwd 破解,假设的前两位是salt import crypt #数据比较 def password_crak(pass_word): salt = pass_word[0: ...

  7. jchdl - RTL实例 - MOS6502 Mem

    https://mp.weixin.qq.com/s/ST8q-VWOT47kcYg10-4AQw   实现一个简单的内存模块,匹配MOS6502 CPU使用.   参考链接 https://gith ...

  8. ASP.NET防止自己网站的资源被盗(通过IHttpHandler 带样例说明)

    我这里用的图片被盗举例子 一个正常的网页 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind ...

  9. Java实现 LeetCode 728 自除数(暴力)

    728. 自除数 自除数 是指可以被它包含的每一位数除尽的数. 例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0. 还有,自除数不允许包含 ...

  10. Java实现 LeetCode 145 二叉树的后序遍历

    145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...