python-3.xpython-requestspython-webbrowser

Problem in opening link via python webbrowser


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>

Solution

  • 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')