【发布时间】: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
【问题讨论】:
-
在 Gspread 文档中找到了一个 clear() 但不确定如何在上面的代码中实现 gspread.readthedocs.io/en/latest/#gspread.Worksheet.clear
-
还有一些 clear() 在这个 github.com/burnash/gspread/blob/master/tests/test.py
标签: python python-2.7 selenium beautifulsoup gspread