如何使用python从公共googlesheets获取数据?
我正在尝试获取以下谷歌表单不同工作表中的新冠病毒-19数据。g-sheet开放供公众使用,URL仅返回第一张工作表。我想刮除所有工作表。有人能帮忙吗。以下是谷歌工作表链接:如何使用python从公共googlesheets获取数据?,python,web-scraping,google-sheets,data-mining,google-sheets-api,Python,Web Scraping,Google Sheets,Data Mining,Google Sheets Api,我正在尝试获取以下谷歌表单不同工作表中的新冠病毒-19数据。g-sheet开放供公众使用,URL仅返回第一张工作表。我想刮除所有工作表。有人能帮忙吗。以下是谷歌工作表链接: 您可以使用请求来完成。所有表都在一个HTML文档的源代码中。只需遍历表并写入CSV即可 from bs4 import BeautifulSoup import csv import requests html = requests.get('https://docs.google.com/spreadsheets/d/e
您可以使用请求来完成。所有表都在一个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
您可以使用请求来完成它。所有表都在一个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
这太好了!!不过,我有几个建议,一个是在
csv.writer()
方法中传递lineterminator='\n'
参数,第二个是在open()
方法中传递encoding='utf8'
。非常感谢dan!这太好了!!不过,我有几个建议,一个是在csv.writer()
方法中传递lineterminator='\n'
参数,第二个是在open()
方法中传递encoding='utf8'
。非常感谢dan!