2017-06-16 4 views
2

私はpythonスクリプトを使用して、一連のウェブサイトURLを含むファイルを取り込み、それらをすべて新しいタブで開きます。しかし、私は最初のウェブサイトを開いて、エラーメッセージが出てい:これは私が得るものです:ウェブサイトオープナーのスクリプト

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.") 

私のファイルはの束で構成されていURLはそれぞれの行にあります。

答えて

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 


 

関連する問題