Python webbrowser模块无法正常工作

时间:2016-06-15 12:35:40

标签: python

我在python中创建这个自动化脚本,它使用字典打开浏览器,当使用webbrowser模块打开解析的字典时,我不断收到一个关键错误。这是代码:

import webbrowser, sys, requests
ptcl = 'https://'
tail = '.com/'
context_dict =
    {
        'fb': ptcl + 'facebook' + tail,
        'ig': ptcl + 'instagram' + tail,
        'google': ptcl + 'google' + tail,
        'kat': ptcl + 'kat.cr',
        'mail': ptcl + 'gmail' + tail,
        'utube': ptcl + 'youtube' + tail,       
        }


def open_page(page):
    webbrowser.open(context_dict[page])
def get_args():
    if len(sys.argv) > 1:
        for i in sys.argv[1:]:
            page = context_dict[i.replace(',', '')]
            open_page(page)

if __name__ == '__main__':
    get_args()

以下是尝试从cmd访问fb时的示例 sample fb run

1 个答案:

答案 0 :(得分:1)

你查找两次:

page = context_dict[i.replace(',', '')]

然后

webbrowser.open(context_dict[page])

删除其中一个查找。

我建议:

def get_args():
    if len(sys.argv) > 1:
        for i in sys.argv[1:]:
            key = i.replace(',', '')
            open_page(key)
相关问题