0

I am trying to download google sheet using belowcode. However it is not giving unexpected result by printing differennt characters into xls sheet. Any idea what could be wrong. Code:

url = 'google_docs_link'
user, password = 'user', 'pwd'
resp = requests.get(url, auth=(user, password))
print(resp)
with open('C:\\AAA\\exportdata.xls', "wb") as o:
    o.write(resp.content)`
print(resp) is giving the output of <Response [200]>
  • Does this answer your question? [Download link for Google Spreadsheets CSV export - with Multiple Sheets](https://stackoverflow.com/questions/33713084/download-link-for-google-spreadsheets-csv-export-with-multiple-sheets) – Zero Jul 07 '23 at 14:16

1 Answers1

0

First off, I am not sure if the way you're using to get the Google Sheet is correct, but assuming that to be correct.

Try print(resp.content) instead of print(resp), and checking the response manually, sometimes the server will return an error but still give response code 200.

Moreover, resp.content will return the bytes and you'll have to encode them into unicode yourself.

So try:

with open('C:\\AAA\\exportdata.xls', "wb", encoding="utf-8") as o:
    o.write(str(resp.content)[2:-1])
Shoto
  • 31
  • 4
  • Google doc link is valid one. When I acccess it through browser it works fine. Tried following: * `print(resp.content)` prints the byte response, however it doesn't seem to have actual response. * **Command:** ``` with open('C:\\AAA\\exportdata.xls', "wb", encoding="utf-8") as o: ``` **Gives Error:** `ValueError: binary mode doesn't take an encoding argument` When I open file in non-binary mode using ``` with open('C:\\AAA\\exportdata.xls', "w", encoding="utf-8") as o: ``` Google login page is shown in the excel sheet instead of actual contents. Any idea what could be wrong? – SumitGandhi Jul 23 '23 at 13:01