如何将数据从 pandas 上传到 Google sheet?
How to upload data from pandas into Google sheet?
我正在尝试使用 pandas 将抓取的数据上传到 google sheet,但出现以下错误:
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
AttributeError: 'list' object has no attribute 'columns'
这是我的代码
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
from google.oauth2.service_account import Credentials
import gspread
scope = ['https://www.googleapis.com/auth/spreadsheets']
creds = Credentials.from_service_account_file("123.json", scopes=scope)
gs = gspread.authorize(creds)
sheet = gs.open_by_url('https://docs.google.com/spreadsheets/d/14jRVDlF1QY-vo0An2T8_aj-8TAgcIgGD3mrbDAjk9_E/edit#gid=0')
worksheet = sheet.get_worksheet(0)
PATH ="C:/Users/XXX/Desktop/chromedriver.exe"
driver = webdriver.Chrome( PATH )
driver.get("https://fixturedownload.com/results/epl-2021")
Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//select[@name='timezone']")))).select_by_value("SE Asia Standard Time" )
driver.find_element(By.XPATH, "//input[@value='Set Timezone']").click()
data = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[2]/div/table"))).get_attribute("outerHTML")
df = pd.read_html(data)
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
根据 pandas 文档:
https://pandas.pydata.org/docs/reference/api/pandas.read_html.html
方法 read_html
returns 数据帧列表,这就是您收到该错误的原因。因此,您需要在代码中更改此设置:
df = pd.read_html(data)
worksheet.update([df[0].columns.values.tolist()] + df[0].values.tolist())
这是假设您只需要列表中的第一个数据帧,否则您将需要遍历数据帧列表:
dfs = pd.read_html(data)
for df in dfs:
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
我正在尝试使用 pandas 将抓取的数据上传到 google sheet,但出现以下错误:
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
AttributeError: 'list' object has no attribute 'columns'
这是我的代码
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
from google.oauth2.service_account import Credentials
import gspread
scope = ['https://www.googleapis.com/auth/spreadsheets']
creds = Credentials.from_service_account_file("123.json", scopes=scope)
gs = gspread.authorize(creds)
sheet = gs.open_by_url('https://docs.google.com/spreadsheets/d/14jRVDlF1QY-vo0An2T8_aj-8TAgcIgGD3mrbDAjk9_E/edit#gid=0')
worksheet = sheet.get_worksheet(0)
PATH ="C:/Users/XXX/Desktop/chromedriver.exe"
driver = webdriver.Chrome( PATH )
driver.get("https://fixturedownload.com/results/epl-2021")
Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//select[@name='timezone']")))).select_by_value("SE Asia Standard Time" )
driver.find_element(By.XPATH, "//input[@value='Set Timezone']").click()
data = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[2]/div/table"))).get_attribute("outerHTML")
df = pd.read_html(data)
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
根据 pandas 文档: https://pandas.pydata.org/docs/reference/api/pandas.read_html.html
方法 read_html
returns 数据帧列表,这就是您收到该错误的原因。因此,您需要在代码中更改此设置:
df = pd.read_html(data)
worksheet.update([df[0].columns.values.tolist()] + df[0].values.tolist())
这是假设您只需要列表中的第一个数据帧,否则您将需要遍历数据帧列表:
dfs = pd.read_html(data)
for df in dfs:
worksheet.update([df.columns.values.tolist()] + df.values.tolist())