我正试图用python在浏览器中打开一个HTML/CSS/JS文件。
这里没问题,我正在发送一个URL,一切都很好但是当我想像这样将参数添加到我的URL时:file:///C:/Users/Me/Desktop/pageHTML.html?x=38
每个浏览器都会这样打开它:file:///C:/Users/Me/Desktop/pageHTML.html
我读到Windows这样做是为了解决奇怪的安全问题
所以我想知道是否有人用Python或JS解决了这个问题?
不幸的是,我无法用本地服务器读取它,这必须是一个本地文件。
好吧,我找到了一种方法,这篇文章已经用java回答了这个问题,但我认为我可以为像我这样在将一种语言翻译成另一种语言方面有问题的新手回答。
这是代码:
import tempfile
import webbrowser
def createRedirectPage(url):
return("<!DOCTYPE HTML>" +
"<meta charset="UTF-8">" +
"<meta http-equiv="refresh" content="1; url=" + url + "">" +
"<script>" +
"window.location.href = "" + url + """ +
"</script>" +
"<title>Page Redirection</title>" +
"<!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->" +
"If you are not redirected automatically, follow the <a href='" + url + "'>link</a>")
def createRedirectTempFile(url):
tmp=tempfile.NamedTemporaryFile(delete=False)
path=tmp.name+'.html'
f=open(path, 'w')
f.write(createRedirectPage(url))
f.close()
webbrowser.open('file://' + path)
createRedirectPage("YourURL")