📜  获取 tld google 表格 (1)

📅  最后修改于: 2023-12-03 14:57:13.091000             🧑  作者: Mango

获取 Google 表格 TLD 数据

如果你想获取 Google 表格中的 TLD(顶级域名)数据,可以使用 Google Sheets API。Google Sheets API 是 Google Sheets 平台的 API,可以用于读取、修改和管理 Google Sheets 文档,同时支持多种编程语言。

步骤 1: 准备工作和授权

首先你需要创建一个 Google Cloud Console 项目,获取项目的 API Key 和 Client ID。详情请参考 Google Sheets API 快速入门指南

接下来需要安装 google-authgoogle-api-python-client Python 模块。

pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
步骤 2: 获取 TLD 数据

要读取 Google Sheets 文档中的 TLD 数据,你需要知道文档的 ID。可以在文档 URL 中找到,如:https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit 中的 ID 是 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms

以下是 Python 代码示例,获取 TLD 数据并打印:

from google.oauth2 import service_account
from googleapiclient.discovery import build

# 服务账号凭证
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SERVICE_ACCOUNT_FILE = 'path/to/service/account.json'
creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# Google Sheets API
service = build('sheets', 'v4', credentials=creds)
sheet_id = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
result = service.spreadsheets().values().get(
    spreadsheetId=sheet_id, range='A2:A').execute()
tlds = result.get('values', [])
for tld in tlds:
    print(tld[0])

注:path/to/service/account.json 是你的服务账号 JSON 文件路径,sheet_id 是你要读取的 Google Sheets 文档 ID,range 是你要读取的数据范围,这里是第一列所有行,从第二行开始(A2:A)。

返回 Markdown 格式
## 获取 Google 表格 TLD 数据

如果你想获取 Google 表格中的 TLD(顶级域名)数据,可以使用 Google Sheets API。Google Sheets API 是 Google Sheets 平台的 API,可以用于读取、修改和管理 Google Sheets 文档,同时支持多种编程语言。

首先你需要创建一个 Google Cloud Console 项目,获取项目的 API Key 和 Client ID。详情请参考 [Google Sheets API 快速入门指南](https://developers.google.com/sheets/api/quickstart/python)。

接下来需要安装 `google-auth` 和 `google-api-python-client` Python 模块。

```python
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client

要读取 Google Sheets 文档中的 TLD 数据,你需要知道文档的 ID。可以在文档 URL 中找到,如:https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit 中的 ID 是 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms

以下是 Python 代码示例,获取 TLD 数据并打印:

from google.oauth2 import service_account
from googleapiclient.discovery import build

# 服务账号凭证
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SERVICE_ACCOUNT_FILE = 'path/to/service/account.json'
creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# Google Sheets API
service = build('sheets', 'v4', credentials=creds)
sheet_id = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
result = service.spreadsheets().values().get(
    spreadsheetId=sheet_id, range='A2:A').execute()
tlds = result.get('values', [])
for tld in tlds:
    print(tld[0])

注:path/to/service/account.json 是你的服务账号 JSON 文件路径,sheet_id 是你要读取的 Google Sheets 文档 ID,range 是你要读取的数据范围,这里是第一列所有行,从第二行开始(A2:A)。