美丽的汤& GSpread将数据附加到Google Sheet,而不是替换

时间:2017-02-08 21:26:31

标签: python python-2.7 selenium beautifulsoup gspread

我的第一个价格检查器监控50个产品并提取数据,每天凌晨3点运行。目前检查的产品附加到现有数据上,这显然会产生重复数据,所以几天后我最终会产生50,100,150行...

如何让Python脚本替换或清除现有数据,因此电子表格中只有50个产品?

以下是代码的顶部:

from selenium import webdriver 
import time
from bs4 import BeautifulSoup


import json
import gspread
#from oauth2client.client import SignedJwtAssertionCredentials
from oauth2client.client import SignedJwtAssertionCredentials
from json import load
import urllib2

browser = webdriver.PhantomJS()

product_details = []



def connect_to_spreadsheet():
    json_key = json.load(open('0b6bb6f4e5.json'))
    scope = ['https://spreadsheets.google.com/feeds']
    credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
    #credentials = AssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
    gc = gspread.authorize(credentials)
    wks = gc.open("TestSheet")

    worksheet = wks.worksheet('automated')
    last_row = worksheet.row_count 
    last_col = worksheet.col_count - 1 

    for each_row_data in product_details:
        try:
            worksheet.append_row(each_row_data)
        except:
            print "Could not add row data", each_row_data

1 个答案:

答案 0 :(得分:0)

clear()清除工作表,但生成的内容在清除之后开始

worksheet = wks.worksheet('automated')
worksheet.clear()

但是明确的是所需的函数(),只需要在A2中启动数据