当我用Python(Windows 10)发送本地文件地址时,我试图找到一种让浏览器读取URL参数的方法

2024-04-25 10:18:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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解决了这个问题

不幸的是,我不能用本地服务器读取它,这必须是一个本地文件


Tags: 文件url参数windowshtmljs浏览器users
1条回答
网友
1楼 · 发布于 2024-04-25 10:18:44

好的,我找到了一个方法,thispost已经用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")

相关问题 更多 >