【问题标题】:Python - how to load Google Chrome or Chromium browser in the gtk.Window like webkit.WebView()?Python - 如何在 gtk.Window 中加载 Google Chrome 或 Chromium 浏览器,如 webkit.WebView()?
【发布时间】:2013-09-15 04:03:57
【问题描述】:

在 Python (Linux) 中,如何在 gtk.Window() 中加载 Google chrome 或 Chromium 浏览器?

我现在在哪里使用 webkit,但由于 Javscript 引擎和其他更新问题,我需要使用 Google Chrome/Chromium 而不是 webkit。

$ apt-get install python-webkit
$ cat >> /var/tmp/browser.py << \EOF
#!/usr/bin/env python
import gtk
import webkit
import gobject
gobject.threads_init()
win = gtk.Window()
win.set_title("Python Browser")
bro = webkit.WebView()
bro.open("http://www.google.com")
win.add(bro)
win.show_all()
gtk.main()

EOF
$ python /var/tmp/browser.py

【问题讨论】:

    标签: python linux google-chrome webkit chromium


    【解决方案1】:

    扩展 sfantu 的答案。 CEF Python 自带了在 PyGTK 应用程序中嵌入 Chrome 浏览器的示例,请看截图:

    https://code.google.com/p/cefpython/wiki/PyGTK

    示例来源(CEF 1 / CEF 3 / Windows / Linux):

    https://code.google.com/p/cefpython/source/browse/cefpython/cef1/windows/binaries/pygtk_.py https://code.google.com/p/cefpython/source/browse/cefpython/cef1/linux/binaries_64bit/pygtk_.py https://code.google.com/p/cefpython/source/browse/cefpython/cef3/windows/binaries/pygtk_.py

    使用您喜欢的任何框架都可以使用 CEF Python 嵌入 Chrome 浏览器(附带 PyGTK、wxPython、PyQt、PySide、Panda3D、Kivy 框架、PyWin32 的示例)。您只需将窗口句柄传递给 CEF,浏览器就会嵌入到该窗口中。在 Linux 上需要传递一个指向 GtkWindow 的指针。

    【讨论】:

    • 我在运行带有 python qt4 绑定的 64 位 Linux 时出现分段错误。例如:paste.ubuntu.com/6158865
    • @YumYumYum 请阅读 pyqt.py 文件顶部的注释 - 此错误是已知的。 Linux 上的 pyqt 示例是前两天才添加的,还没有经过全面测试。需要进一步调试才能解决此问题。 pyqt 示例在 Windows 上运行良好。
    • @YumYumYum 您可以从 Windows 复制 PyGTK CEF 3 示例,它应该在经过一些修改后在 Linux 上运行,您必须设置“locales_dir_path”和“resources_dir_path”选项,请参见此处的第 484 行: code.google.com/p/cefpython/source/browse/cefpython/cef3/linux/… 。还将脚本开头的“libcef.dll”更改为“libcef.so”。如果您遇到任何问题,请在 CEF Python 论坛上创建一个主题。
    • @YumYumYum 我已经为 pyqt 错误创建了Issue 88 - 为它加星标,当它得到修复时,您会收到电子邮件通知。
    【解决方案2】:

    我认为您不能嵌入 Chrome ...您可以在 Qt 中创建应用程序并嵌入 QtWebkit ...或者您可以将 selenium 与您希望的任何驱动程序(包括 Chrome)一起使用,但我认为您不能嵌入那个。

    Qtwebkit 拥有您需要的所有功能。

    编辑

    我收回了一切,因为我刚刚发现了一些可能有用的东西。 :D

    https://bitbucket.org/chromiumembedded/ “一个用于在其他应用程序中嵌入 Chromium 浏览器窗口的简单框架。”

    而且这个框架也有 python 绑定: http://code.google.com/p/cefpython/

    但我不确定 Chromium 是否具有您需要的所有功能...

    【讨论】:

    • 但是使用 QtWebKit 我们不能使用最新的 JavaScript 引擎 + WebRTC + Chrome/Chromium/Google Chrome 中提供的更多功能。
    • 同样使用 QtWebKit,我们无法使用 Chrome 的某些功能,例如:Google Hangout 和许多其他模块。
    • 不,我没有测试过
    【解决方案3】:

    根据PyGTK FAQ,有可能。

    tutorial 中的更多信息。

    【讨论】:

      【解决方案4】:

      使用

      #!/usr/bin/python
      # -*- coding: utf-8 -*-
      import platform
      import os
      
      name = "Google Chrome/Chromium"
      
      
      def find_path():
          """
          find the chrome executable path on Windows, Linux/OpenBSD, Mac or Unknown system.
          """
          if platform.system() == "Windows":
              return _find_chrome_win()
      
          elif platform.system() == "Darwin":  # Mac OS
              return _find_chrome_mac()
      
          elif platform.system() == "Linux" or platform.system() == "OpenBSD":
              return _find_chrome_linux()
      
          else:
              try:
                  return _find_chrome_linux()
              except:
                  try:
                      return _find_chrome_mac()
                  except:
                      try:
                          return _find_chrome_win()
                      except:
                          return None
      
      
      def _find_chrome_mac():
          paths = [  # mac os chrome path list
              "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
              "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
              "/Applications/Chromium.app/Contents/MacOS/Chromium",
              "/usr/bin/google-chrome-stable",
              "/usr/bin/google-chrome",
              "/usr/bin/chromium",
              "/usr/bin/chromium-browser",
          ]
      
          chrome_path = None
      
          for path in paths:  # loop through paths
              if os.path.exists(path):
                  chrome_path = path
      
          if chrome_path != None:
              return chrome_path
      
          else:  # use which command to find chrome
              for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):
                  a = os.popen("which " + browser).read()
                  if a == "":
                      pass
                  else:
                      return a[:-1]
      
              return None
      
      
      def _find_chrome_linux():
          paths = [
              "/usr/bin/google-chrome-stable",  # linux chrome path list
              "/usr/bin/google-chrome",
              "/usr/bin/chromium",
              "/usr/bin/chromium-browser",
              "/snap/bin/chromium",
          ]
      
          chrome_path = None
      
          for path in paths:
              if os.path.exists(path):
                  chrome_path = path
      
          if chrome_path != None:
              return chrome_path
      
          else:  # use which command to find chrome
              for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):
                  a = os.popen("which " + browser).read()
                  if a == "":
                      pass
                  else:
                      return a[:-1]
      
              return None
      
      
      def _find_chrome_win():
          import winreg as reg  # import registry
      
          chrome_path = None
          reg_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe"
      
          for install_type in (reg.HKEY_CURRENT_USER, reg.HKEY_LOCAL_MACHINE):
              try:
                  reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
                  chrome_path = reg.QueryValue(reg_key, None)
                  reg_key.Close()
                  if not os.path.isfile(chrome_path):
                      continue
              except WindowsError:
                  chrome_path = None
              else:
                  break
      
          for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):
              a = os.popen("where " + browser).read()
              if a == "":
                  pass
              else:
                  chrome_path = a[:-1]
      
          return chrome_path
      
      
      chrome_path = find_path()
      if not chrome_path:
          raise Exception("No chrome")
      
      
      class ChromeView:
          def __init__(self, url="data:text/html,<h1>DicksonUI</h1>", options=[]):
              cmd = chrome_path
              for option in options:
                  cmd += " "
                  cmd += option
              cmd += " --incognito"
              cmd += " --new-window"
              cmd += ' --app="'
              cmd += url + '"'
              os.popen(cmd)
      
          def version(self, path):
              try:
                  v = os.popen(find_path() + " --version").read()
                  v2 = v[v.find(" ") + 1 :]
                  return int(v2[: v2.find(".")])
              except:
                  return None
      
          def is_chromium(self, path):
              try:
                  if os.popen(path + " --version").read().startswith("Chromium"):
                      return True
                  else:
                      return False
              except:
                  return None
      
      ChromeView("https://www.google.com")
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-22
        • 1970-01-01
        • 2018-08-24
        • 1970-01-01
        • 2022-08-04
        • 1970-01-01
        • 2013-10-02
        相关资源
        最近更新 更多