我不明白为什么如果我启动它,在 github 中填写注册表并点击“注册”发布数据是 {}。我正在使用 python 3.7、cefpython 66.0 代码:
import tkinter as tk
from tkinter import messagebox
from cefpython3 import cefpython as cef
#import cefpython3
import threading
import sys
import time
import webbrowser
def on_browse(browser,frame,request,user_gesture,is_redirect):
if user_gesture == True:
print('user_gesture:',user_gesture)
print('GetFirstPartyForCookies:',request.GetFirstPartyForCookies())
print('GetFlags',request.GetFlags())
print('GetHeaderMap',request.GetHeaderMap())
print('GetHeaderMultimap',request.GetHeaderMultimap())
print('GetMethod',request.GetMethod())
print('GetPostData',request.GetPostData())
print('GetUrl',request.GetUrl())
def test_thread(frame):
global browser
sys.excepthook = cef.ExceptHook
window_info = cef.WindowInfo(frame.winfo_id())
window_info.SetAsChild(frame.winfo_id(), rect)
cef.Initialize()
browser = cef.CreateBrowserSync(window_info, url='http://www.github.com')
browser.SetClientCallback('OnBeforeBrowse',on_browse)
cef.MessageLoop()
def on_closing():
print('closing')
r.destroy()
r = tk.Tk()
r.geometry('800x600')
r.protocol('WM_DELETE_WINDOW', on_closing)
frame = tk.Frame(r, bg='blue', height=400)
frame2 = tk.Frame(r, bg='white', height=200)
frame.pack(side='top', fill='x')
frame2.pack(side='top', fill='x')
r.update()
tk.Button(frame2, text='Exit', command=on_closing).pack(side='left')
tk.Button(frame2, text='Show something',
command=lambda: messagebox.showinfo('TITLE', 'Shown something')).pack(side='right')
rect = [0, 0, frame.winfo_width(), frame.winfo_height()]
print('browser: ', rect[2], 'x', rect[3])
thread = threading.Thread(target=test_thread, args=(frame,))
thread.start()
r.mainloop()
browser: 800 x 400
user_gesture: True
GetFirstPartyForCookies:
GetFlags 0
GetHeaderMap {}
GetHeaderMultimap []
GetMethod POST
GetPostData {}
GetUrl https://github.com/join
closing
>>>
我期待在 GetPostData 中找到一些东西。感谢所有会帮助我的人!
****更新****建议我尝试使用官方cefpython tkinter示例。我添加了
self.browser.SetClientCallback('OnBeforeResourceLoad',self.on_browse)
在类 browserFrame 中定义 embed_browser。然后我在“def embed_browser”之前添加这个函数
def on_browse(self, browser,frame,request):
user_gesture = True
if user_gesture == True:
print('user_gesture:',user_gesture)
print('GetFirstPartyForCookies:',request.GetFirstPartyForCookies())
print('GetFlags',request.GetFlags())
print('GetHeaderMap',request.GetHeaderMap())
print('GetHeaderMultimap',request.GetHeaderMultimap())
print('GetMethod',request.GetMethod())
print('GetPostData',request.GetPostData())
print('GetUrl',request.GetUrl())
我得到了这个输出:
user_gesture: True
GetFirstPartyForCookies: https://github.com/join
GetFlags 0
GetHeaderMap {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Content-Type': 'application/x-www-form-urlencoded', 'Origin': 'https://github.com', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'}
GetHeaderMultimap [('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'), ('Content-Type', 'application/x-www-form-urlencoded'), ('Origin', 'https://github.com'), ('Upgrade-Insecure-Requests', '1'), ('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36')]
GetMethod POST
[CEF Python] ExceptHook: catched exception, will shutdown CEF
Traceback (most recent call last):
File "request_handler.pyx", line 117, in cefpython_py37.RequestHandler_OnBeforeResourceLoad
File "C:\Users\*****\Desktop\tkinter_.py", line 166, in on_browse
print('GetPostData',request.GetPostData())
File "request.pyx", line 83, in cefpython_py37.PyRequest.GetPostData
File "request.pyx", line 122, in cefpython_py37.PyRequest.GetPostData
File "C:\Python37\lib\urllib\parse.py", line 740, in parse_qsl
value = _coerce_result(value)
File "C:\Python37\lib\urllib\parse.py", line 103, in _encode_result
return obj.encode(encoding, errors)
UnicodeEncodeError: 'ascii' codec can't encode character '\u2713' in position 0: ordinal not in range(128)