- 安装32位版本的python 3.6
- 使用官网提供的node.dll (miniblink_x64.dll匹配64位python)
- 地址:git clone https://github.com/lochen88/MBPython.git
# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@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 = 5
while mb.wkeIsLoading(webview)==0 and times>0:
time.sleep(1)
times -= 1
if times <= 0:
break
cookie = 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 = "http://www.toutiao.com/c/user/token/MS4wLjABAAAAwsW0adFE7nxniVhSEmzCJDz9mWXZnLuXH9oSb-Un-_A/"
url2 = random.choice(URL_LIST)
del_cookie(mb2,webview2)
# cookie = 's_v_web_id=verify_ld2vqow9_jMA18r5I_gQZv_4Ti9_9z71_WfhCgCFce8z8'
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,flag
times = 5
while mb2.wkeIsLoading(webview2)==0 and times>0:
time.sleep(1)
times-=1
if times<=0:
break
cookie = get_cookie(webview2)
print("cookie2 = ", cookie)
check_verify(webview2,2)
save_html(webview2)
del_cookie(mb2,webview2)
window2.wkeDestroyWebView(webview2)
print("关闭")
flag = True
def check_verify(webview,id):
html = str(mb.wkeGetSource(webview), encoding="utf-8")
if "captcha_container" in html:
print("{} 存在验证码".format(id))
return True
else:
if "404" not in html:
print("{}不存在验证码".format(id))
else:
print("页面加载404")
return False
def 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)
# print("get_cookie",cookie)
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):
# cookie1 = "'s_v_web_id = verify_ld2vqow9_jMA18r5I_gQZv_4Ti9_9z71_WfhCgCFce8z9'"
# print("'"+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)
# mb.wkeSetCookieEnabled(webview, True)
# url = "http://www.toutiao.com/c/user/token/MS4wLjABAAAAVCjGiw2zI7Y8d-vgbr7hhwQlkvmD3T6cWm_qjFr8M4k/"
# 获取url
url = random.choice(URL_LIST)
# 删除cookie
del_cookie(mb,webview)
# 设置新cookie
cookie = random.choice(COOKIE_LIST)
print("cookie",cookie)
set_cookie(webview, url, cookie)
# 加载url
mb.wkeLoadURLW(webview, url)
# show 窗口
window.wkeShowWindow(webview)
# 加载完成回调 onLoadingFinish1
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:
break
mb.wkeFinalize()
if __name__ == '__main__':
flag = False
all_num = 0
os.makedirs("./html",exist_ok=True)
mbpython = miniblink.Miniblink
mb = mbpython.init('node.dll')
wke = mbpython(mb)
window = wke.window
mbpython2 = miniblink.Miniblink
mb2 = mbpython2.init('node.dll')
wke2 = mbpython(mb2)
window2 = wke2.window
ck = 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())
# 禁止多线程调用
# from threading import Thread
# # while not flag:
# Thread(target=main1,args=(mb,window)).start()
while not flag:
all_num+=1
print("启动")
main1(mb,window)
flag = False
print("*"*50+str(all_num)+"*"*50)
requirement
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156873.html