如何使用urllib打开自定义协议url?

2024-09-29 07:29:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用chrome Momentum扩展来定制我的浏览器新选项卡,并想编写一个python脚本来获得它的每日仪表板墙纸

现在我知道我可以通过url访问所需页面

chrome-extension://laookkfknpbbblfpciffpaejjkokdgca/dashboard.html

但是,当我尝试使用此url调用urllib.request.urlopen时,出现以下错误:

urllib.error.URLError: <urlopen error unknown url type: chrome-extension>

是否可以包含由urllib打开的自定义协议

或者是否有其他方法获得页面html结果


Tags: 脚本url墙纸htmlextension浏览器仪表板error
1条回答
网友
1楼 · 发布于 2024-09-29 07:29:31

如果该文件存在于本地而不在web上,那么使用urllib不会对您有多大好处,因为它不是URL

改用webbrowser库并提供文件的路径:


    def auto_open():
        """
        This method takes the absolute path to the html file and opens it directly in the browser.
        """
        html_page = 'path/to/your/file'
        # open in a new tab.
        new = 2
        webbrowser.open(html_page, new=new)

相关问题 更多 >