Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python从公共googlesheets获取数据?_Python_Web Scraping_Google Sheets_Data Mining_Google Sheets Api - Fatal编程技术网

如何使用python从公共googlesheets获取数据?

如何使用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

我正在尝试获取以下谷歌表单不同工作表中的新冠病毒-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

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