【问题标题】:How to extract data from a google spreadsheet to notebook如何从谷歌电子表格中提取数据到笔记本
【发布时间】:2020-03-27 17:39:43
【问题描述】:

我想从这个每 5 分钟更新一次的Google Spreadsheet 中提取有关印度所有 CoVid19 病例的数据。 以前我曾从 API 中提取数据,但不知道如何从电子表格中提取数据。

【问题讨论】:

    标签: python api web-scraping data-science


    【解决方案1】:

    链接:

    https://docs.google.com/spreadsheets/d/1cB7QX00wceTIrEYix5q3eZfWepAbr4C_oTT6wDth-IA/edit?usp=sharing

    API 密钥:you_need_your_own_API_key

    #import library
    import os
    cwd = os.getcwd()
    cwd
    
    import gspread
    #Service client credential from oauth2client
    from oauth2client.service_account import ServiceAccountCredentials
    # Print nicely
    import pprint
    #Create scope
    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    #create some credential using that scope and content of startup_funding.json
    creds = ServiceAccountCredentials.from_json_keyfile_name('startup_funding.json',scope)
    #create gspread authorize using that credential
    client = gspread.authorize(creds)
    #Now will can access our google sheets we call client.open on StartupName
    sheet = client.open('Test_Sheet').sheet1
    pp = pprint.PrettyPrinter()
    results = sheet.get_all_records()
    results
    
    
    Final Result:
    
    [{'Name': 'Kim K.', 'Address': '10 Main St.', 'Age': 22},
     {'Name': 'Ralph G.', 'Address': '20 Killmore La.', 'Age': 56},
     {'Name': 'Timmy H.', 'Address': '56 Freemont Pl.', 'Age': 32},
     {'Name': 'Fred A.', 'Address': '71 South Terr.', 'Age': 41},
     {'Name': 'Danny S.', 'Address': '99 Penny La', 'Age': 62}]
    
    
    Or, more specific ranges:
    result = sheet.row_values(1) #See individual row
    result
    result = sheet.col_values(1) #See individual column
    result
    result = sheet.cell(5,2) # See particular cell
    result
    

    参考资料:

    https://medium.com/datadriveninvestor/use-google-sheets-as-your-database-using-python-77d40009860f

    https://gspread.readthedocs.io/en/latest/user-guide.html#creating-a-spreadsheet

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多