def login(driver, filename): logger.info('准备更新cookie') # screen_shot = driver.save_screenshot('bin/1.png') WebDriverWait(driver, 10).until( ec.presence_of_element_located((By.XPATH, r'//*[@id="login-username"]'))) username = driver.find_element_by_xpath(r'//*[@id="login-username"]') username.send_keys(engine.user_name) password = driver.find_element_by_xpath('//*[@id="login-passwd"]') password.send_keys(engine.pass_word) driver.find_element_by_class_name("btn-login").click() # logger.info('第四步') # try: cracker = slider_cracker(driver) cracker.crack() # except: # logger.exception('出错') time.sleep(5) if driver.title == '投稿 - 哔哩哔哩弹幕视频网 - ( ゜- ゜)つロ 乾杯~ - bilibili': cookie = driver.get_cookies() print(cookie) with open(filename, "w") as f: json.dump(cookie, f) logger.info('更新cookie成功') else: logger.info('更新cookie失败')
def upload(self, title_, file_list, link): filename = 'engine/bilibili.cookie' # title_ = self.r_title videopath = self.assemble_videopath(file_list) # service_log_path = "{}/chromedriver.log".format('/home') options = webdriver.ChromeOptions() options.add_argument('headless') driver = webdriver.Chrome(executable_path=engine.chromedrive_path, chrome_options=options) # service_log_path=service_log_path) try: driver.get("https://www.bilibili.com") # driver.delete_all_cookies() if os.path.isfile(filename): with open(filename) as f: new_cookie = json.load(f) for cookie in new_cookie: # print(cookie) driver.add_cookie(cookie) driver.get("https://member.bilibili.com/video/upload.html") # print(driver.title) WebDriverWait(driver, 20).until( ec.presence_of_element_located((By.NAME, 'buploader'))) upload = driver.find_element_by_name('buploader') # print(driver.title) # logger.info(driver.title) upload.send_keys(videopath) # send_keys logger.info('开始上传' + title_) time.sleep(2) button = r'//*[@class="new-feature-guide-v2-container"]/div/div/div/div/div[1]' if self.is_element_exist(driver, button): sb = driver.find_element_by_xpath(button) sb.click() sb.click() sb.click() logger.debug('点击') while True: # info = driver.find_elements_by_xpath( # '//*[@id="app"]/div[3]/div[2]/div[2]/div[2]/div/div/div[2]/div[2]') # print(info) info = driver.find_elements_by_class_name(r'item-upload-info') for t in info: # print(t) if t.text != '': print(t.text) # else: # print('出问题啦') time.sleep(10) # text = driver.find_elements_by_xpath( # '//*[@id="app"]/div[3]/div[2]/div[2]/div[2]/div/div/div[2]/div[2]/span') text = driver.find_elements_by_xpath(r'//*[@class="item-upload-info"]/span') aggregate = set() for s in text: if s.text != '': aggregate.add(s.text) print(s.text) # if text == 'Upload complete' or text == '上传完成': # break if len(aggregate) == 1 and ('Upload complete' in aggregate or '上传完成' in aggregate): break logger.info('上传%s个数%s' % (title_, len(info))) # js = "var q=document.getElementsByClassName('content-header-right')[0].scrollIntoView();" # driver.execute_script(js) cookie = driver.get_cookies() with open(filename, "w") as f: json.dump(cookie, f) # 点击模板 driver.find_element_by_xpath(r'//*[@class="normal-title-wrp"]/div/p').click() driver.find_element_by_class_name(r'template-list-small-item').click() # driver.find_element_by_xpath( # r'//*[@id="app"]/div[3]/div[2]/div[3]/div[1]/div[1]/div/div[2]/div[1]').click() # 输入转载来源 input_o = driver.find_element_by_xpath( '//*[@class="upload-v2-container"]/div[2]/div[3]/div[1]/div[4]/div[3]/div/div/input') input_o.send_keys(link) # 选择分区 # driver.find_element_by_xpath(r'//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div[3]/div').click() # driver.find_element_by_xpath(r'//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]/div[6]').click() # 稿件标题 title = driver.find_element_by_xpath( '//*[@class="upload-v2-container"]/div[2]/div[3]/div[1]/div[8]/div[2]/div/div/input') title.send_keys(Keys.CONTROL + 'a') title.send_keys(Keys.BACKSPACE) title.send_keys(title_) # js = "var q=document.getElementsByClassName('content-tag-list')[0].scrollIntoView();" # driver.execute_script(js) # time.sleep(3) # 输入相关游戏 # driver.save_screenshot('bin/err.png') # print('截图') # text_1 = driver.find_element_by_xpath( # '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[5]/div/div/div[1]/div[2]/div/div/input') # text_1.send_keys('星际争霸2') # 简介 text_2 = driver.find_element_by_xpath( '//*[@class="upload-v2-container"]/div[2]/div[3]/div[1]/div[12]/div[2]/div/textarea') text_2.send_keys('职业选手直播第一视角录像。这个自动录制上传的小程序开源在Github:' 'http://t.cn/RgapTpf(或者在Github搜索ForgQi)交流群:837362626' '\n顺便推广一下自己的网站http://web-form.me/') driver.find_element_by_xpath('//*[@class="upload-v2-container"]/div[2]/div[3]/div[5]/span[1]').click() # screen_shot = driver.save_screenshot('bin/1.png') # print('截图') time.sleep(3) upload_success = driver.find_element_by_xpath(r'//*[@id="app"]/div/div[3]/h3').text if upload_success == '': driver.save_screenshot('err.png') logger.info('稿件提交失败,截图记录') return else: logger.info(upload_success) # print('稿件提交完成!') # logger.info('%s提交完成!' % title_) self.remove_filelist(file_list) except selenium.common.exceptions.NoSuchElementException: logger.exception('发生错误') # except selenium.common.exceptions.TimeoutException: # logger.exception('超时') except selenium.common.exceptions.TimeoutException: logger.info('准备更新cookie') # screen_shot = driver.save_screenshot('bin/1.png') WebDriverWait(driver, 10).until( ec.presence_of_element_located((By.XPATH, r'//*[@id="login-username"]'))) username = driver.find_element_by_xpath(r'//*[@id="login-username"]') username.send_keys(engine.user_name) password = driver.find_element_by_xpath('//*[@id="login-passwd"]') password.send_keys(engine.pass_word) driver.find_element_by_class_name("btn-login").click() # logger.info('第四步') # try: cracker = slider_cracker(driver) cracker.crack() # except: # logger.exception('出错') time.sleep(5) if driver.title == '投稿 - 哔哩哔哩弹幕视频网 - ( ゜- ゜)つロ 乾杯~ - bilibili': cookie = driver.get_cookies() print(cookie) with open(filename, "w") as f: json.dump(cookie, f) logger.info('更新cookie成功') else: logger.info('更新cookie失败') finally: driver.quit() logger.info('浏览器驱动退出')
def upload(self, title_, file_list, link): filename = 'engine/bilibili.cookie' # title_ = self.r_title videopath = self.assemble_videopath(file_list) # service_log_path = "{}/chromedriver.log".format('/home') options = webdriver.ChromeOptions() options.add_argument('headless') driver = webdriver.Chrome(executable_path=engine.chromedrive_path, chrome_options=options) # service_log_path=service_log_path) try: driver.get("https://www.bilibili.com") # driver.delete_all_cookies() if os.path.isfile(filename): with open(filename) as f: new_cookie = json.load(f) for cookie in new_cookie: # print(cookie) driver.add_cookie(cookie) driver.get("https://member.bilibili.com/video/upload.html") # print(driver.title) self.add_videos(driver, title_, videopath) # js = "var q=document.getElementsByClassName('content-header-right')[0].scrollIntoView();" # driver.execute_script(js) cookie = driver.get_cookies() with open(filename, "w") as f: json.dump(cookie, f) self.add_information(driver, link, title_) driver.find_element_by_xpath( '//*[@class="upload-v2-container"]/div[2]/div[3]/div[5]/span[1]' ).click() # screen_shot = driver.save_screenshot('bin/1.png') # print('截图') time.sleep(3) upload_success = driver.find_element_by_xpath( r'//*[@id="app"]/div/div[3]/h3').text if upload_success == '': driver.save_screenshot('err.png') logger.info('稿件提交失败,截图记录') return else: logger.info(upload_success) # print('稿件提交完成!') # logger.info('%s提交完成!' % title_) self.remove_filelist(file_list) except selenium.common.exceptions.NoSuchElementException: logger.exception('发生错误') # except selenium.common.exceptions.TimeoutException: # logger.exception('超时') except selenium.common.exceptions.TimeoutException: logger.info('准备更新cookie') # screen_shot = driver.save_screenshot('bin/1.png') WebDriverWait(driver, 10).until( ec.presence_of_element_located( (By.XPATH, r'//*[@id="login-username"]'))) username = driver.find_element_by_xpath( r'//*[@id="login-username"]') username.send_keys(engine.user_name) password = driver.find_element_by_xpath('//*[@id="login-passwd"]') password.send_keys(engine.pass_word) driver.find_element_by_class_name("btn-login").click() # logger.info('第四步') # try: cracker = slider_cracker(driver) cracker.crack() # except: # logger.exception('出错') time.sleep(5) if driver.title == '投稿 - 哔哩哔哩弹幕视频网 - ( ゜- ゜)つロ 乾杯~ - bilibili': cookie = driver.get_cookies() print(cookie) with open(filename, "w") as f: json.dump(cookie, f) logger.info('更新cookie成功') else: logger.info('更新cookie失败') finally: driver.quit() logger.info('浏览器驱动退出')