【问题标题】:Beautiful Soup & GSpread appending data to Google Sheet, instead of replacingBeautiful Soup & GSpread 将数据附加到 Google 表格,而不是替换
【发布时间】:2017-06-26 16:59:50
【问题描述】:

我的第一个价格检查器可以监控 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

【问题讨论】:

标签: python python-2.7 selenium beautifulsoup gspread


【解决方案1】:

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

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

但明确是需要的function(),只需要数据在A2开始

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    相关资源
    最近更新 更多