英文:
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('response.html', 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)
webbrowser.open('response.html')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论