Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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 编写网站开启器脚本_Python_File Io_Python Webbrowser - Fatal编程技术网

Python 编写网站开启器脚本

Python 编写网站开启器脚本,python,file-io,python-webbrowser,Python,File Io,Python Webbrowser,我正在使用python脚本接收一个包含大量网站URL的文件,并在新选项卡中打开所有这些URL。但是,我在打开第一个网站时收到一条错误消息:这是我得到的: 0:41:执行错误: “不理解“打开位置”消息。(-1708) 到目前为止,我的脚本如下所示: import os import webbrowser websites = [] with open("websites.txt", "r+") as my_file: websites.append(my_file.readline())

我正在使用python脚本接收一个包含大量网站URL的文件,并在新选项卡中打开所有这些URL。但是,我在打开第一个网站时收到一条错误消息:这是我得到的:

0:41:执行错误: “不理解“打开位置”消息。(-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组成,它们各自的行。

我试着运行你的代码,它在我的机器上使用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


希望这有帮助