[問題] 該如何寫入google試算表的指定位置呢?

作者: kiwigo1000 (快變小黑人了.....)   2023-03-17 15:40:54
我從這個日本旅遊網站[步步日本]的首頁,抓取了文章的標題與簡介的內容,然後使用
pandas來彙整。之後想要將彙整的資料輸出到google線上試算表單上面。
https://www.bubu-jp.com/
請問我該如何將pandas的彙整內容,寫入試算表單的指定位置呢?
譬如說,不從試算表A1開始寫入,而是想從B5這一格開始寫入的話,該怎麼寫呢?
不曉得最後一行的code該如何寫才好...
附上code:
import requests
from bs4 import BeautifulSoup
import pandas as pd
import gspread
from google.colab import auth
auth.authenticate_user()
import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)
wb = gc.open_by_key('google試算表的ID')
ws = wb.worksheet('sheet名稱')
url = "https://www.bubu-jp.com/"
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
articles = soup.find_all("div", {"class": "p-blog-archive__item-info"})
result =[]
for article in articles:
title = article.find("h2",{"class":"p-blog-archive__item-title
p-article__title"}).getText()
content = article.find("p",{"class":"p-blog-archive__item-excerpt
u-hidden-xs"}).getText()
result.append([title,content])
df = pd.DataFrame(result, columns=["標題", "簡介"])
ws.update([df.columns.values.tolist()] + df.values.tolist())
作者: yiche (你若安好,便是晴天)   2023-03-17 18:17:00
作者: pov (pov)   2023-04-01 14:39:00
pygsheet

Links booklink

Contact Us: admin [ a t ] ucptt.com