生活随笔
收集整理的這篇文章主要介紹了
pyhton-miniblink
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
安裝32位版本的python 3.6使用官網提供的node.dll (miniblink_x64.dll匹配64位python)地址:git clone https://github.com/lochen88/MBPython.git
"""
@author : v_jiaohaicheng@baidu.com
@des : miniblink-pyhton 使用樣例"""
from MBPython
import miniblink
,cookie
from ctypes
import *
import ctypes
, ctypes
.wintypes
import time
import re
import os
import datetime
import random
@CFUNCTYPE(None,c_int
)
def onLoadingFinish1(webview
):global mb
,window
,flag
print("調用界面1")times
= 5while mb
.wkeIsLoading
(webview
)==0 and times
>0:time
.sleep
(1)times
-= 1if times
<= 0:breakcookie
= get_cookie
(webview
)print("cookie1 = ", cookie
)status
= check_verify
(webview
,1)save_html
(webview
)del_cookie
(mb
, webview
)window
.wkeDestroyWebView
(webview
)if status
:webview2
= window2
.wkeCreateWebWindow
(0, 0, 0, 0, 1366, 768)url2
= random
.choice
(URL_LIST
)del_cookie
(mb2
,webview2
)cookie
= random
.choice
(COOKIE_LIST
)set_cookie
(webview
, url2
, cookie
)mb
.wkeLoadURLW
(webview2
, url2
)window2
.wkeShowWindow
(webview2
)mb
.wkeOnLoadingFinish
(webview2
, onLoadingFinish2
, 0)else:flag
= True@CFUNCTYPE(None,c_int
)
def onLoadingFinish2(webview2
):print("調用界面2")global mb2
,window2
,flagtimes
= 5while mb2
.wkeIsLoading
(webview2
)==0 and times
>0:time
.sleep
(1)times
-=1if times
<=0:breakcookie
= get_cookie
(webview2
)print("cookie2 = ", cookie
)check_verify
(webview2
,2)save_html
(webview2
)del_cookie
(mb2
,webview2
)window2
.wkeDestroyWebView
(webview2
)print("關閉")flag
= Truedef check_verify(webview
,id):html
= str(mb
.wkeGetSource
(webview
), encoding
="utf-8")if "captcha_container" in html
:print("{} 存在驗證碼".format(id))return Trueelse:if "404" not in html
:print("{}不存在驗證碼".format(id))else:print("頁面加載404")return Falsedef save_html(webview
):save_time
= str(datetime
.datetime
.now
()).split
(".")[0].replace
(":", "-").replace
(" ", "")with open("./html/{}.html".format(save_time
), "w", encoding
="utf-8")as fp
:fp
.write
(str(mb
.wkeGetSource
(webview
), encoding
="utf-8"))def get_cookie(webview
):cookie
= ck
.wkeGetCookieW
(webview
)s_v_web_id
= re
.search
("s_v_web_id=(.*);|s_v_web_id=(.*)|s_v_web_id = (.*?);|s_v_web_id = (.*)", cookie
)if s_v_web_id
:return s_v_web_id
.group
()else:return ""def set_cookie(webview
, url
, cookie
):ck
.wkeSetCookie
(webview
=webview
, url
=url
,cookie
=cookie
)def del_cookie(mb
,webview
):mb
.wkeClearCookie
(webview
)cookie_file
= R"./cookie.dat"if os
.path
.exists
(cookie_file
):os
.remove
(cookie_file
)def main1(mb
,window
):webview
= window
.wkeCreateWebWindow
(0, 0, 0, 0, 1366, 768)url
= random
.choice
(URL_LIST
)del_cookie
(mb
,webview
)cookie
= random
.choice
(COOKIE_LIST
)print("cookie",cookie
)set_cookie
(webview
, url
, cookie
)mb
.wkeLoadURLW
(webview
, url
)window
.wkeShowWindow
(webview
)mb
.wkeOnLoadingFinish
(webview
, onLoadingFinish1
, 0)msg
= ctypes
.wintypes
.MSG
()lpMsg
= ctypes
.byref
(msg
)while ctypes
.windll
.user32
.GetMessageW
(lpMsg
, 0, 0, 0) > 0:ctypes
.windll
.user32
.TranslateMessage
(lpMsg
)ctypes
.windll
.user32
.DispatchMessageW
(lpMsg
)if flag
== True:breakmb
.wkeFinalize
()if __name__
== '__main__':flag
= Falseall_num
= 0os
.makedirs
("./html",exist_ok
=True)mbpython
= miniblink
.Miniblinkmb
= mbpython
.init
('node.dll')wke
= mbpython
(mb
)window
= wke
.windowmbpython2
= miniblink
.Miniblinkmb2
= mbpython2
.init
('node.dll')wke2
= mbpython
(mb2
)window2
= wke2
.windowck
= cookie
.Cookie
(mb
)URL_LIST
= []with open("./url.txt","r",encoding
="utf-8")as fp
:for i
in fp
:URL_LIST
.append
(i
.strip
())COOKIE_LIST
= []with open("./cookie.txt", "r", encoding
="utf-8")as fp
:for i
in fp
:COOKIE_LIST
.append
(i
.strip
())while not flag
:all_num
+=1print("啟動")main1
(mb
,window
)flag
= Falseprint("*"*50+str(all_num
)+"*"*50)
requirement
總結
以上是生活随笔為你收集整理的pyhton-miniblink的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。