Pythonでseleiumを使ってgoogleスプレッドシートに記入するには

pythonとseleniumでスクレイピングしたデータをスプレッドシートに書き込む方法です。
備忘録として置いときます。

from selenium import webdriver
from time import sleep
import chromedriver_binary 
from selenium.common.exceptions import NoSuchElementException
import gspread
import json
from oauth2client.service_account import ServiceAccountCredentials 


scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('---------', scope)
#OAuth2を使ってgoogleAPIにログインします。
gc = gspread.authorize(credentials)
#スプレッドシートキーはurlのd/と/editの間の文字列
SPREADSHEET_KEY = '---------------'

wb = gc.open_by_key(SPREADSHEET_KEY)
#ワークブックのワークシートを選択
ws = wb.worksheet("ワークシートの名前")

num = 2
url = "ターゲットサイトのurl"
driver = webdriver.Chrome()
driver.get(url)
sleep(3)

ws.update_acell('A1', "number")
ws.update_acell('B1', "title")

contents = driver.find_elements_by_xpath('ターゲットコンテンツ')
for content in contents:
  text = content.text
  print(text)
  ws.update_acell(("A" + str(num)),num - 1)
  ws.update_acell(("B" + str(num)),text)
  num += 1