问题在于通过Python的webbrowser打开链接时。

huangapple go评论53阅读模式
英文:

Problem in opening link via python webbrowser

问题

当我通过Postman发送GET请求时,有一个选项来发送并下载文件(一个HTML文件),该文件可以在浏览器中打开并执行。我希望通过以下Python代码以相同的方式执行:

from requests import get as GET
import webbrowser

tokenUrl = '...'
tokenParams = {}
r = GET(tokenUrl, params=tokenParams)
print(r.text)
webbrowser.open(r.text)

然而,这会在浏览器中显示一个错误,显示"文件未找到"错误。完整的错误消息如下:

Firefox can’t find the file at currentpath/<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"><script>function redirectOnLoad() {if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://login.salesforce.com/?ec=302&startURL=/setup/secur/RemoteAccessAuthorizationPage.apexp?source=CAAAAYYrFX9vMDAwMDAwMDAwMDAwMDAwAAAA8EXaTiy4GdL6nQvL92NfYNg7dzLv4kyxjuZcHKHgNWQQqNF3iKqic0U94dDZSTuDkdwQlxNr5Iqs91cDizSR_QSWdQfg7Gv8wEkB-WyB_2D-57w3Glc28imEFrJSi3M3Xzf6JxZHMkfwRE4T2w5lYTAie-_LbnGu_BDzvlBF-VsyQeufgzH0x36e1zmxy4ef0pp3jjYOtWY9TGpHmURyoroEYE7cBMRXbkXdmGt_umLePR4lBzpAlKGI1q06FoQePUNAn_VAxZfUu8bw3KtdIrsLMxbNQH6aEDv29tCZJiZR274d5BE_GgRX-4r54Sw-6QhLs0XkcSOKO1ASX2Rm8tL5o-9SGYtlpVY8o9be3HoKAz4_HVqqMj45uUCvt-cUXp3wELtsZJmPC-pzTV0pdYymHpm4QiWn7g14CNz5OVi0anBwBeeytE1z-zEsKWdrnIrJO1Gkzbst_SIewi61lrwa0ZSg5TJmSsq3JRTI_AavON_rFG98ZHgie6Ow6keDVlgBgb154xDI-I3mw8NzVCWbXDKXbisOvAMqbaURUf67_XMHjhU-4h-PXtLM-k3jJrccvsf7E-clBSBJzBsfvUA%3D'); } else if (window.location.replace){ window.location.replace('https://login.salesforce.com/?ec=302&startURL=/setup/secur/RemoteAccessAuthorizationPage.apexp?source=CAAAAYYrFX9vMDAwMDAwMDAwMDAwMDAwAAAA8EXaTiy4GdL6nQvL92NfYNg7dzLv4kyxjuZcHKHgNWQQqNF3iKqic0U94dDZSTuDkdwQlxNr5Iqs91cDizSR_QSWdQfg7Gv8wEkB-WyB_2D-57w3Glc28imEFrJSi3M3Xzf6JxZHMkfwRE4T2w5lYTAie-_LbnGu_BDzvlBF-VsyQeufgzH0x36e1zmxy4ef0pp3jjYOtWY9TGpHmURyoroEYE7cBMRXbkXdmGt_umLePR4lBzpAlKGI1q06FoQePUNAn_VAxZfUu8bw3KtdIrsLMxbNQH6aEDv29tCZJiZR274d5BE_GgRX-4r54Sw-6QhLs0XkcSOKO1ASX2Rm8tL5o-9SGYtlpVY8o9be3HoKAz4_HVqqMj45uUCvt-cUXp3wELtsZJmPC-pzTV0pdYymHpm4QiWn7g14CNz5OVi0anBwBeeytE1z-zEsKWdrnIrJO1Gkzbst_SIewi61lrwa0ZSg5TJmSsq3JRTI_AavON_rFG98ZHgie6Ow6keDVlgBgb154xDI-I3mw8NzVCWbXDKXbisOvAMqbaURUf67_XMHjhU-4h-PXtLM-k3jJrccvsf7E-clBSBJzBsfvUA%3D');} else {window.location.href ='https://login.salesforce.com/?ec=302&startURL=/setup/secur/RemoteAccessAuthorizationPage.apexp?source=CAAAAYYrFX9vMDAwMDAwMDAwMDAwMDAwAAAA8EXaTiy4GdL6nQvL92NfYNg7dzLv4kyxjuZcHKHgNWQQqNF3iKqic0U94dDZSTuDkdwQlxNr5Iqs91cDizSR_QSWdQfg7Gv8wEkB-WyB_2D-57w3Glc28imEFrJSi3M3Xzf6JxZHMkfwRE4T2w5lYTAie-_LbnGu_BDzvlBF-VsyQeufgzH0x36e1zmxy4ef0pp3jjYOtWY9TGpHmURyoroEYE7cBMRXbkXdmGt_umLePR4lBzpAlKGI1q06FoQePUNAn_VAxZfUu8bw3KtdIrsLMxbNQH6aEDv29tCZJiZR274d5BE_GgRX-4r54Sw-6QhLs0XkcSOKO1ASX2Rm8tL5o-9SGYtlpVY8o9be3HoKAz4_HVqqMj45uUCvt-cUXp3wELtsZJmPC-pzTV0pdYymHpm4QiW

<details>
<summary>英文:</summary>

When I send the GET request via postman there is an option to send and download and the download file(an html file) is executable which opens In a browser.
I wish to do the same via python via following code.

from requests import get as GET
import webbrowser

tokenUrl = '...'
tokenParams = {}
r = GET(tokenUrl,params = tokenParams)
print(r.text)
webbrowser.open(r.text)

However this displays an error in browser saying filenotfound error.
Complete error message:

Firefox can’t find the file at currentpath/<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"><script>function redirectOnLoad() {if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://login.salesforce.com/?ec=302&startURL=/setup/secur/RemoteAccessAuthorizationPage.apexp?source=CAAAAYYrFX9vMDAwMDAwMDAwMDAwMDAwAAAA8EXaTiy4GdL6nQvL92NfYNg7dzLv4kyxjuZcHKHgNWQQqNF3iKqic0U94dDZSTuDkdwQlxNr5Iqs91cDizSR_QSWdQfg7Gv8wEkB-WyB_2D-57w3Glc28imEFrJSi3M3Xzf6JxZHMkfwRE4T2w5lYTAie-_LbnGu_BDzvlBF-VsyQeufgzH0x36e1zmxy4ef0pp3jjYOtWY9TGpHmURyoroEYE7cBMRXbkXdmGt_umLePR4lBzpAlKGI1q06FoQePUNAn_VAxZfUu8bw3KtdIrsLMxbNQH6aEDv29tCZJiZR274d5BE_GgRX-4r54Sw-6QhLs0XkcSOKO1ASX2Rm8tL5o-9SGYtlpVY8o9be3HoKAz4_HVqqMj45uUCvt-cUXp3wELtsZJmPC-pzTV0pdYymHpm4QiWn7g14CNz5OVi0anBwBeeytE1z-zEsKWdrnIrJO1Gkzbst_SIewi61lrwa0ZSg5TJmSsq3JRTI_AavON_rFG98ZHgie6Ow6keDVlgBgb154xDI-I3mw8NzVCWbXDKXbisOvAMqbaURUf67_XMHjhU-4h-PXtLM-k3jJrccvsf7E-clBSBJzBsfvUA%3D'); } else if (window.location.replace){ window.location.replace('https://login.salesforce.com/?ec=302&startURL=/setup/secur/RemoteAccessAuthorizationPage.apexp?source=CAAAAYYrFX9vMDAwMDAwMDAwMDAwMDAwAAAA8EXaTiy4GdL6nQvL92NfYNg7dzLv4kyxjuZcHKHgNWQQqNF3iKqic0U94dDZSTuDkdwQlxNr5Iqs91cDizSR_QSWdQfg7Gv8wEkB-WyB_2D-57w3Glc28imEFrJSi3M3Xzf6JxZHMkfwRE4T2w5lYTAie-_LbnGu_BDzvlBF-VsyQeufgzH0x36e1zmxy4ef0pp3jjYOtWY9TGpHmURyoroEYE7cBMRXbkXdmGt_umLePR4lBzpAlKGI1q06FoQePUNAn_VAxZfUu8bw3KtdIrsLMxbNQH6aEDv29tCZJiZR274d5BE_GgRX-4r54Sw-6QhLs0XkcSOKO1ASX2Rm8tL5o-9SGYtlpVY8o9be3HoKAz4_HVqqMj45uUCvt-cUXp3wELtsZJmPC-pzTV0pdYymHpm4QiWn7g14CNz5OVi0anBwBeeytE1z-zEsKWdrnIrJO1Gkzbst_SIewi61lrwa0ZSg5TJmSsq3JRTI_AavON_rFG98ZHgie6Ow6keDVlgBgb154xDI-I3mw8NzVCWbXDKXbisOvAMqbaURUf67_XMHjhU-4h-PXtLM-k3jJrccvsf7E-clBSBJzBsfvUA%3D');} else {window.location.href ='https://login.salesforce.com/?ec=302&startURL=/setup/secur/RemoteAccessAuthorizationPage.apexp?source=CAAAAYYrFX9vMDAwMDAwMDAwMDAwMDAwAAAA8EXaTiy4GdL6nQvL92NfYNg7dzLv4kyxjuZcHKHgNWQQqNF3iKqic0U94dDZSTuDkdwQlxNr5Iqs91cDizSR_QSWdQfg7Gv8wEkB-WyB_2D-57w3Glc28imEFrJSi3M3Xzf6JxZHMkfwRE4T2w5lYTAie-_LbnGu_BDzvlBF-VsyQeufgzH0x36e1zmxy4ef0pp3jjYOtWY9TGpHmURyoroEYE7cBMRXbkXdmGt_umLePR4lBzpAlKGI1q06FoQePUNAn_VAxZfUu8bw3KtdIrsLMxbNQH6aEDv29tCZJiZR274d5BE_GgRX-4r54Sw-6QhLs0XkcSOKO1ASX2Rm8tL5o-9SGYtlpVY8o9be3HoKAz4_HVqqMj45uUCvt-cUXp3wELtsZJmPC-pzTV0pdYymHpm4QiWn7g14CNz5OVi0anBwBeeytE1z-zEsKWdrnIrJO1Gkzbst_SIewi61lrwa0ZSg5TJmSsq3JRTI_AavON_rFG98ZHgie6Ow6keDVlgBgb154xDI-I3mw8NzVCWbXDKXbisOvAMqbaURUf67_XMHjhU-4h-PXtLM-k3jJrccvsf7E-clBSBJzBsfvUA%3D';} } redirectOnLoad();</script></head></html><!-- Body events --><script type="text/javascript">function bodyOnLoad(){if(window.PreferenceBits){window.PreferenceBits.prototype.csrfToken="null";};}function bodyOnBeforeUnload(){}function bodyOnFocus(){}function bodyOnUnload(){}</script></body></html>


</details>


# 答案1
**得分**: 0

我发现[如何处理响应][1]的请求文档对解决错误很有帮助。
现在的代码看起来如下:

```python
r = GET(tokenUrl, params=tokenParams)

with open('response.html', 'wb') as fd:
    for chunk in r.iter_content(chunk_size=128):
        fd.write(chunk)
webbrowser.open('response.html')
英文:

I found the requests' documentation on how to handle response helpful in resolving the error.
Code now looks as

    r = GET(tokenUrl,params = tokenParams)

    with open(&#39;response.html&#39;, &#39;wb&#39;) as fd:
        for chunk in r.iter_content(chunk_size=128):
            fd.write(chunk)
    webbrowser.open(&#39;response.html&#39;)

huangapple
  • 本文由 发表于 2023年2月6日 16:56:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定