python - 如何使用 python 从公共(public)谷歌表中获取数据?

标签 python web-scraping google-sheets data-mining google-sheets-api

我正在尝试获取以下谷歌工作表的不同工作表中存在的 COVID-19 数据。 g-sheet 开放供公众使用,URL 仅返回第一个工作表。我想抓取所有工作表。任何人都可以提供帮助。这是谷歌表格链接:

https://docs.google.com/spreadsheets/d/e/2PACX-1vSc_2y5N0I67wDU38DjDh35IZSIS30rQf7_NYZhtYYGU1jJYT6_kDx4YpF-qw0LSlGsBYP8pqM_a1Pd/pubhtml

最佳答案

您可以使用请求来完成。所有表格都在一个 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/

相关文章:

python - 在 pandas DataFrame 中查找条件连续值

python - 使用 NumPy 进行位破解

python - 使用 python/urllib2 重定向后如何确定最终 URL?

python - Scrapy Xpath 输出为空

javascript - 重定向后先等待再执行操作 - Puppeteer Node.js

python - 在广泛的 Scrapy Web 爬虫中只允许内部链接

javascript - 不工作 : Code to send email reminder based on date

javascript - 将电子邮件发送到多个地址的脚本

python - Django/MySQL 中的间隙检测

google-apps-script - sheet.appendRow() 偶尔会以静默方式失败并导致空行