本人正在寫一個selenium爬蟲的網(wǎng)頁面板,用flask控制selenium的一些行為。目標(biāo)網(wǎng)頁需要掃描二維碼登陸,現(xiàn)在卡在掃描二維碼完成登陸后這個階段,因?yàn)橄氩坏饺绾卧趺磳?shí)現(xiàn)登錄后進(jìn)行狀態(tài)輪詢,并返回到Flask進(jìn)而觸發(fā)網(wǎng)頁元素變化。具體代碼如下:
from flask import Flask,render_template,request,redirect,url_for
from Control import control
from flask import session,escape
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'dqwer235r*tbqew4r1$1232~@'
test = control()
socketio = SocketIO(app)
@app.route('/',methods=['GET','POST'])
def login():
try:
if request.method == 'POST':
uname = request.form['username']
passwd = request.form['password']
if uname == 'admin' and passwd == '123456':
session['username'] = 'admin'
return redirect(url_for('dashboard'))
except Exception as e:
return render_template('homepage.html',error=e)
return render_template('homepage.html')
@app.route('/dashboard/',methods=['GET','POST'])
def dashboard():
if request.method == 'GET'and request.headers.get("Referer") == 'http://127.0.0.1/':
if 'username' in session:
if session['username'] == 'admin':
if 'QRstatus' in session and session['QRstatus'] == 'True':
if test.ck_login():
return render_template('dashboard.html',msg='Logined.',qrSrc=test.ck_login())
else:
session['QRstatus'] = 'False'
else:
# TODO: Add not login status
return render_template('dashboard.html')
else:
return redirect(url_for('login'))
elif request.method == 'POST':
if request.form['submit'] == 'Start':
if 'QRstatus' in session and session['QRstatus'] == 'True':
return render_template('dashboard.html',msg='QR code was printed.',qrSrc=test.ck_login())
msg = 'Started.'
qrSrc = test.qr()
session['QRstatus'] = 'True'
return render_template('dashboard.html',msg=msg,qrSrc=str(qrSrc))
else:
msg = 'no'
return render_template('dashboard.html',msg=msg)
return render_template('dashboard.html')
if __name__ == '__main__':
socketio.run(app,port=80)
selenium的部分:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,StaleElementReferenceException
class img_url_match(object):
def __init__(self,locator,text_):
self.locator = locator
self.text = text_
def __call__(self, driver):
try:
img_src = EC._find_element(driver,self.locator).get_attribute('src')
return img_src.startswith(self.text)
except StaleElementReferenceException:
return False
class control:
def __init__(self):
self.driver = webdriver.Chrome()
def qr(self):
self.driver.get('http://example.com')
try:
while True :
if not WebDriverWait(self.driver,2).until(img_url_match((By.ID,'js_login_qrcode_img'),'data:image/gif;base64')):
continue
else:
qr_src = self.driver.find_element_by_id('js_login_qrcode_img').get_attribute('src')
return qr_src
except TimeoutException:
self.driver.refresh()
def ck_login(self):
if self.driver.current_url == 'http://example.com':
try:
url = WebDriverWait(self.driver,25).until(EC.url_changes('http://example.com'))
return 'data:image/gif;base64,' +self.driver.get_screenshot_as_base64()
except TimeoutException:
return False
elif self.driver.current_url == 'http://example.com/home/':
return 'data:image/gif;base64,' + self.driver.get_screenshot_as_base64()
elif self.driver.current_url == ':data:,':
print('Now the url is :' + self.driver.current_url)
WebDriverWait(self.driver,5).until(EC.presence_of_element_located((By.ID,'js-ch-member-face')))
return 'data:image/gif;base64,' + self.driver.get_screenshot_as_base64()
if __name__ == '__main__':
test = control()
print(test.qr())
現(xiàn)在我打算用協(xié)程或者subprocess來進(jìn)行selenium某些狀態(tài)的輪詢,但不知這樣會不會啟動另一個新的selenium實(shí)例,即開啟一個新的瀏覽器?另外使用tornado會不會好些?
謝謝大家。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。