cefpython3 獲取頁面加載過程中的所有訪問鏈接

目前cefpython3的最新版本是 v66.0 ,最高支持python3.7 .

 

API:

https://github.com/cztomczak/cefpython/blob/master/api/API-categories.md

"""
Implement RequestHandler.CanGetCookies and CanSetCookie
to block or allow cookies over network requests.
"""

from cefpython3 import cefpython as cef


def main():
    cef.Initialize()
    browser = cef.CreateBrowserSync(
        url="http://www.baidu.com",
        window_title="baidu")
    browser.SetClientHandler(RequestHandler())
    cef.MessageLoop()
    del browser
    cef.Shutdown()


class RequestHandler(object):
    def __init__(self):
        self.getcount = 0
        self.setcount = 0

    def GetResourceHandler(self, frame, request, **_):
        print("GetResourceHandler url="+request.GetUrl())

    def CanGetCookies(self, frame, request, **_):
        return True
        # There are multiple iframes on that website, let's log
        # cookies only for the main frame.
        if frame.IsMain():
            self.getcount += 1
            print("-- CanGetCookies #"+str(self.getcount))
            print("url="+request.GetUrl()[0:80])
            print("")
        # Return True to allow reading cookies or False to block
        return True

    def CanSetCookie(self, frame, request, cookie, **_):
        return True
        # There are multiple iframes on that website, let's log
        # cookies only for the main frame.
        if frame.IsMain():
            self.setcount += 1
            print("-- CanSetCookie @"+str(self.setcount))
            print("url="+request.GetUrl()[0:80])
            print("Name="+cookie.GetName())
            print("Value="+cookie.GetValue())
            print("")
        # Return True to allow setting cookie or False to block
        return True


if __name__ == '__main__':
    main()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章