序言:
好久沒有更新博客了,最近在工作中碰到這種需求,由於沒有做過,中間碰到好多坑,最后在一位貴人幫助的情況下,最終還是搞定了。
第一步,先安裝 cefpython3
pip install cefpython3
然后 運行下這塊代碼
from cefpython3 import cefpython as cef import platform import sys from configparser import ConfigParser def main(): siteUrl = 'https://www.baidu.com/' title = '百度一下,你就知道' check_versions() sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error cef.Initialize() """ conf = ConfigParser() conf.read("config.ini",'utf-8') siteUrl = conf.get("main","url") title = conf.get("main","title") print(siteUrl) print(title) """ cef.CreateBrowserSync(url=siteUrl, window_title=title) cef.MessageLoop() cef.Shutdown() def check_versions(): ver = cef.GetVersion() print("[hello_world.py] CEF Python {ver}".format(ver=ver["version"])) print("[hello_world.py] Chromium {ver}".format(ver=ver["chrome_version"])) print("[hello_world.py] CEF {ver}".format(ver=ver["cef_version"])) print("[hello_world.py] Python {ver} {arch}".format( ver=platform.python_version(), arch=platform.architecture()[0])) assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this" if __name__ == '__main__': main()
是不是突然間 彈出了一窗口
好,要的就是這種效果,但問題來了,我們如何在沒有python 環境的電腦上運行呢?接着看......
說白了就是要把 把這個python 腳本打包,打包過程中需要用到 python 的 ,向下看:
pip install pyinstaller
pip install pycrypto
如果順利安裝完成,過程中沒有報錯,好可以繼續了,如果卡到第二個包無法安裝,那么也正常,我裝這個包裝了好久,最終都沒安裝成功,因為在打包過程中編譯時需要依賴 Microsoft visual c++ 14.0 環境,我這邊反正是安裝了但是 pycrypto 還是無法安裝,就報這個錯誤。
我建議大家 還是安裝 anaconda ,然后用這個安裝這個包,記得以前用這個安裝過 scrapy 爬蟲框架,就是用它搞定的。
anaconda 的 具體使用方法可參考 https://blog.csdn.net/ITLearnHall/article/details/81708148 ,當然,自己百度也行。
這幾個包安裝完成之后,我們就可以打包了,來 上這位老哥的git 地址。
https://gitee.com/zhangleijay/cefpython_browser
必須感謝這位老哥,在出差期間 教我怎么打包,非常感謝,拉下來看說明 直接運行就可以
這個包打完之后,我們就看到下面這些文件了,點擊exe文件。
是不是 非常 666
文章最后,送大家幾個字 越努力 越幸運