2017-06-16 47 views
2

我正在使用python脚本来接收包含一堆网站网址的文件,并在新标签中打开所有文件。但是,我越来越开放的第一个网站时的错误信息:这是我得到:使用脚本开启网站

0:41: execution error: " https://www.pandora.com/ " doesn’t understand the “open location” message. (-1708)

我的脚本到目前为止是这样的:

import os 
import webbrowser 
websites = [] 
with open("websites.txt", "r+") as my_file: 
    websites.append(my_file.readline()) 
for x in websites: 
    try: 
     webbrowser.open(x) 
    except: 
     print (x + " does not work.") 

我的文件包含了一堆的自己的网址。

回答

1

我试图运行你的代码和它的作品在我的机器上使用Python 2.7.9

这可能是当你试图打开该文件

字符编码的问题,这是我的建议有以下编辑:

 

import webbrowser 

with open("websites.txt", "r+") as sites: 
    sites = sites.readlines()  # readlines returns a list of all the lines in your file, this makes code more concise 
            # In addition we can use the variable 'sites' to hold the list returned to us by the file object 'sites.readlines()' 


print sites    # here we send the output of the list to the shell to make sure it contains the right information 

for url in sites: 
    webbrowser.open_new_tab(url.encode('utf-8')) # this is here just in-case, to encode characters that the webbrowser module can interpret 
                  # sometimes special characters like '\' or '/' can cause issues for us unless we encode/decode them or make them raw strings 


 

希望这有助于!