2017-02-08 10 views
0

最初の価格チェッカーで50個の製品を監視してデータを取得しました。これは毎日午前3時に実行されます。現時点では、チェックされた製品は明らかに重複を生成している既存のデータに追加されていますので、数日かけて50,100,150行になります...Beautiful Soup&GSpread交換する代わりにGoogleシートにデータを追加

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 
+0

Gspreadのドキュメントではclear()が見つかりましたが、上記のコードで実装する方法は不明https://gspread.readthedocs.io/en/latest/#gspread Worksheet.clear – me9867

+0

といくつかのclear()はhttps://github.com/burnash/gspread/blob/master/tests/test.pyにあります – me9867

答えて

0

クリア()は、シートをクリアしますが、生成されたコンテンツは、それが

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

をクリアした場所後に開始しかし、明確な関数であります()が必要です。A2から開始するデータが必要です

関連する問題