2012-07-05 34 views

回答

4

我自己用这个:(我写的很快,因为它只是为了个人使用)。随着大量清理,你可能会得到你想要的。见https://developers.google.com/chrome-developer-tools/docs/remote-debugging

import urllib2 
import urllib 
import os 
import subprocess 
import json 

from websocket import create_connection 

def refresh_page(url): 
    data = json.load(urllib2.urlopen('http://localhost:9222/json')) 

    found_page = False 
    for page in data: 
     if page['url'].lower() == url.lower(): 
      found_page = True 
      websocketURL = page['webSocketDebuggerUrl'] 
      ws = create_connection(websocketURL) 

      obj = { "id": 0, 
        "method": "Page.reload", 
        "params": 
        { 
         "ignoreCache": True, 
         "scriptToEvaluateOnLoad": "" 
        } 
        } 

      dev_request = json.dumps(obj) 
      ws.send(dev_request) 
      result = ws.recv() 
      ws.close() 
    if not found_page: 
     raise Exception("No pageFound") 

def open_or_refresh(file_name): 
    file_name = "".join ([f if f in r'\/:*?"<>|' else urllib.quote(f) for f in file_name]) 
    file_name = 'file:///' + file_name.replace('\\', '/') 
    file_name = file_name.encode('ascii', 'ignore') 
    try: 
     refresh_page(file_name) 
    except: 
     cmd = (r'"%(LOCALAPPDATA)s\Google\Chrome\Application\chrome.exe"'%os.environ 
       + r' --remote-debugging-port=9222 "%s"' % file_name) 
     subprocess.Popen(cmd) 

open_or_refresh(r"C:\test.html") 
open_or_refresh(r"C:\test.html") 
+0

我得到这个错误:[http://pastie.org/4208613](http://pastie.org/4208613) – prongs 2012-07-06 06:17:40

+0

我不知道发生了什么错误,也许另一个问题(一英镑正在一个发誓的罐子里,但我只能说它对我有用。) – 2012-07-06 09:19:14

+0

如果我将open_or_refresh(r“C:\ test.html”)更改为“open_or_refresh(u”C :\\ test.html“)'你需要字符串在ascii中。 – 2012-07-09 10:12:51