我正在尝试获取以下谷歌工作表的不同工作表中存在的 COVID-19 数据。 g-sheet 开放供公众使用,URL 仅返回第一个工作表。我想抓取所有工作表。任何人都可以提供帮助。这是谷歌表格链接:
最佳答案
您可以使用请求来完成。所有表格都在一个 HTML 文档的源代码中。只需遍历表格并写入 CSV。
from bs4 import BeautifulSoup
import csv
import requests
html = requests.get('https://docs.google.com/spreadsheets/d/e/2PACX-1vSc_2y5N0I67wDU38DjDh35IZSIS30rQf7_NYZhtYYGU1jJYT6_kDx4YpF-qw0LSlGsBYP8pqM_a1Pd/pubhtml').text
soup = BeautifulSoup(html, "lxml")
tables = soup.find_all("table")
index = 0
for table in tables:
with open(str(index) + ".csv", "w") as f:
wr = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
wr.writerows([[td.text for td in row.find_all("td")] for row in table.find_all("tr")])
index = index + 1
关于python - 如何使用 python 从公共(public)谷歌表中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61152242/