selenium+chrome headless完成自動百度八斗平臺的錄入問題(python版本為2.7)
阿新 • • 發佈:2018-11-26
# -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import os from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options import time chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("window-size=1024,768") chrome_options.add_argument("--no-sandbox") base_url = "http://badou.baidu.com/login" #對應的chromedriver的放置目錄 driver = webdriver.Chrome(executable_path=('/usr/local/nginx/html/selenium/chromedriver'), chrome_options=chrome_options) #開啟url driver.get(base_url + "/") #開始 start_time=time.time() print('this is start_time ',start_time) #登入 輸入框 userinput_js = "var userName_node = document.getElementsByName('userName')[0];var password_node = document.getElementsByName('password')[0];userName_node.value = '你的賬號';password_node.value = '你的密碼';" print(userinput_js) driver.execute_script(userinput_js) #登入 確認 login_js = "document.getElementsByTagName('input')[18].click();" print(login_js) driver.execute_script(login_js) #睡眠3秒 截圖 time.sleep(3) driver.save_screenshot('screen.png') #驗證碼 print('寫上你自己的圖片地址url') imgcode = raw_input('請輸入看到的驗證碼:') verify_input_js = "var verify_node = document.getElementsByName('verifyCode')[0];verify_node.value='"+imgcode+"'" driver.execute_script(verify_input_js) #截圖 檢視步驟 driver.save_screenshot('screen.png') print('寫上你自己的圖片地址url') tmp=raw_input('輸入任意值繼續.') #登入之後跳轉 print('跳轉開始...') jump_js = "var pass_btn = document.getElementsByClassName('pass-button pass-button-submit')[0];pass_btn.click();" driver.execute_script(jump_js) time.sleep(3) close_js = "document.getElementsByClassName('el-dialog__close el-icon el-icon-close')[0].click()" driver.execute_script(close_js) driver.save_screenshot('screen.png') #動態令牌 verify = raw_input('請輸入動態令牌:') verify_js = "var verify_node = document.getElementsByClassName('forceverify-input forceverify-input-vcode')[1];verify_node.value='"+verify+"'" driver.execute_script(verify_js) #最終登入 jump_js = "document.getElementsByClassName('forceverify-button forceverify-button-submit')[4].click();" driver.execute_script(jump_js) time.sleep(3) driver.save_screenshot('screen.png') #錄入 問題 with open('./badou.txt','r') as f: line = f.readline() title = line title = title.strip() commit_js = "var object2str = function(obj){var str_arr = [];var i = 0;for(var key in obj){str_arr[i] = key+'='+obj[key]; i++;}return str_arr.join('&');};var ajax_post = function (url, data, fn) {var xhr = new XMLHttpRequest();xhr.open('POST', url, true);xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function()"+" {"+"if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {fn.call(this, xhr.responseText);}"+"};console.log(object2str(data));xhr.send(object2str(data));};var post_url = 'http://badou.baidu.com/api/question/insertUpdate';var data = {'class_id':16,'content':'','cover':'[]',ext:{"+"},'qr_type':6,'title':'"+title+"'};ajax_post(post_url,data,function(res){var string = JSON.stringify(res);var data1 = {'msg' : '問題標題:'+data.title+'返回結果:'+string};var url = 'http://ad.adstart.cn/log_in.php';ajax_post(url,data1,function(res1){console.log(res1);})});document.getElementsByTagName('a')[2].click();" # driver.execute_script(commit_js) print(commit_js) time.sleep(10) f.close() #關閉driver driver.close() #結束 end_time=time.time() print('this is end_time ',end_time)