Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
你能用Python获取webbrowser.get的内容吗?_Python_Python Webbrowser - Fatal编程技术网

你能用Python获取webbrowser.get的内容吗?

你能用Python获取webbrowser.get的内容吗?,python,python-webbrowser,Python,Python Webbrowser,在使用webbrowser.get之后,我是否可以获取有关“a”变量内容的信息?除了“webbrowser.WindowsDefault对象位于0x024643B0”之外,我无法获取任何内容。我想知道哪个浏览器是默认浏览器,并将此信息传输到字符串 import webbrowser a = webbrowser.get('windows-default') print a a.open('http://www.google.com') 以下是两步解决方案: 通过运行以下命令获取浏览器对象的类名

在使用webbrowser.get之后,我是否可以获取有关“a”变量内容的信息?除了“webbrowser.WindowsDefault对象位于0x024643B0”之外,我无法获取任何内容。我想知道哪个浏览器是默认浏览器,并将此信息传输到字符串

import webbrowser
a = webbrowser.get('windows-default')
print a
a.open('http://www.google.com')

以下是两步解决方案:

  • 通过运行以下命令获取浏览器对象的类名:
    type(a)。\uuuuu class\uuuu
  • 创建一个字典,将浏览器类名映射到浏览器类型名
  • 细节: 当您运行
    a=webbrowser.get('windows-default')
    时,您可以通过以下方式获取a的类名:
    browser\u class=type(a)。\uu class\uuu

    基于,您可以对2中突出显示的方法进行编码,如下所示:

    def which_browser(browser_class):
       return{
           'Mozilla': 'firefox',
           'Chrome' : 'google-chrome'           
       }.get(browser_class,'firefox')
    
    此方法根据您在上面的
    browser\u class
    中获得的浏览器类名返回浏览器类型名称。请注意,默认情况下,我将方法设置为return,但对于简单的测试来说,这并不一定。我的意思是,你可以简单地运行:

    def which_browser(browser_class):
       return{
           'Mozilla': 'firefox',
           'Chrome' : 'google-chrome'           
       }
    
    然后,您可以调用
    哪个浏览器()
    方法来获取浏览器类型名称:

    browser_name = which_browser(browser_class)
    
    浏览器名称的类型为
    str
    ,如您所需

    您可能需要此信息链接到上面:

    完整程序: 根据我在Linux机器上的测试,我只安装了2个浏览器,测试了以下代码:

    import webbrowser
    
    a = webbrowser.get('windows-default')
       
    def which_browser(b):
       return{
           'Mozilla': 'firefox',
           'Chrome' : 'google-chrome'           
       }.get(b,'firefox')
    
    print which_browser(type(a).__class__)
    

    运行程序后,我得到以下输出:
    firefox

    在0x024643B0处打印一个.namereturnwebbrowser.WindowsDefault对象是什么?它不工作,它只返回字典,我可能是错的,但看起来你在程序中已经给出了答案,而不是询问答案。什么不工作?不,我不会事先给这个节目注入答案。你说程序会返回字典,这意味着你甚至没有尝试运行它,祝你好运。