How do you save a Google Sheets file as CSV from Python 3 (or 2)?

前端 未结 2 2034
离开以前
离开以前 2021-01-02 19:18

I am looking for a simple way to save a csv file originating from a published Google Sheets document? Since it\'s published, it\'s accessible through a direct link (modified

2条回答
  •  攒了一身酷
    2021-01-02 19:57

    Google responds to the initial request with a series of cookie-setting 302 redirects. If you don't store and resubmit the cookies between requests, it redirects you to the login page.

    So, the problem is not with the User-Agent header, it's the fact that by default, urllib.request.urlopen doesn't store cookies, but it will follow the HTTP 302 redirects.

    The following code works just fine on a public spreadsheet available at the location specified by DOC_URL:

    >>> from http.cookiejar import CookieJar
    >>> from urllib.request import build_opener, HTTPCookieProcessor
    >>> opener = build_opener(HTTPCookieProcessor(CookieJar()))
    >>> resp = opener.open(DOC_URL)
    >>> # should really parse resp.getheader('content-type') for encoding.
    >>> csv_content = resp.read().decode('utf-8')
    

    Having shown you how to do it in vanilla python, I'll now say that the Right Way™ to go about this is to use the most-excellent requests library. It is extremely well documented and makes these sorts of tasks incredibly pleasant to complete.

    For instance, to get the same csv_content as above using the requests library is as simple as:

    >>> import requests
    >>> csv_content = requests.get(DOC_URL).text
    

    That single line expresses your intent more clearly. It's easier to write and easier to read. Do yourself - and anyone else who shares your codebase - a favor and just use requests.

提交回复
热议问题