gpt4 book ai didi

python - 编写网站开启程序

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:36 26 4
gpt4 key购买 nike

我正在使用 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 组成,它们各占一行。

最佳答案

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


希望这对您有所帮助!

关于python - 编写网站开启程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44596579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com