简体   繁体   English

Beautiful Soup和GSpread将数据附加到Google表格,而不是替换

[英]Beautiful Soup & GSpread appending data to Google Sheet, instead of replacing

Got my first price checker going that monitors 50 products and pulls data, this runs at 3am every day. 我有第一个价格检查器,它可以监视50种产品并提取数据,每天凌晨3点运行。 At the moment the products checked are appending to the existing data which is obviously producing duplicates so over a few days I will end up with 50, 100, 150 rows... 目前检查的产品将附加到现有数据上,这显然会产生重复,因此几天后,我将得到50、100、150行...

How can I get the Python script to replace or clear the existing data, so the spreadsheet will only ever have 50 products in it? 如何获取Python脚本来替换或清除现有数据,因此电子表格中只能包含50种产品?

Here is the top part of the code: 这是代码的顶部:

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

The clear() clears the sheet, but the generated content starts after where it was cleared clear()清除工作表,但是生成的内容在清除后开始

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

But clear is the function() required, just need data to start in A2 但明确的是function()是必需的,只需要数据即可在A2中启动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM