Download google docs public spreadsheet to csv with python

前端 未结 4 1816
北荒
北荒 2020-12-14 22:19

I can download a CSV file from Google Docs with wget:

wget --no-check-certificate --output-document=locations.csv \'https://docs.google.com/spre         


        
4条回答
  •  生来不讨喜
    2020-12-14 23:01

    You're not storing cookies.

    First let me say that I completely endorse the recommendation to use the most-excellent requests library.

    However, if you need to do this in vanilla Python 2, the problem lies in the fact that Google is bouncing you around via HTTP 302 redirects, and it expects you to remember the cookies it's setting with each response. When it detects that you aren't storing cookies, it redirects you to the login page.

    By default, urllib2.urlopen (or the opener returned from build_opener) will follow 302 redirects, but it won't store HTTP cookies. You have to teach your opener how to do that. Like so:

    >>> from cookielib import CookieJar
    >>> from urllib2 import build_opener, HTTPCookieProcessor
    >>> opener = build_opener(HTTPCookieProcessor(CookieJar()))
    >>> resp = opener.open('https://docs.google.com/spreadsheet/ccc?key=0ArM5yzzCw9IZdEdLWlpHT1FCcUpYQ2RjWmZYWmNwbXc&output=csv')
    >>> data = resp.read()
    

    Again, use requests if at all possible, but if it's not possible, the standard library can get the job done.

提交回复
热议问题