加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 通过命令行的Windows chrome刷新选项卡0(或当前选项

发布时间:2020-12-16 21:59:43 所属栏目:Python 来源:网络整理
导读:我正在尝试使用python,webbrowser模块.但它没有特定于Chromium的功能.还有另外一种方法吗?可能有一个批处理脚本? 最佳答案 我自己用它:(我写的很快,因为它只是供个人使用).通过大量清理,您可以获得您想要的东西.见https://developers.google.com/chrome-de

我正在尝试使用python,webbrowser模块.但它没有特定于Chromium的功能.还有另外一种方法吗?可能有一个批处理脚本?

最佳答案
我自己用它:(我写的很快,因为它只是供个人使用).通过大量清理,您可以获得您想要的东西.见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)sGoogleChromeApplicationchrome.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")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读