读取Google Sheets数据插入PostgreSQL时遇HTTPS URL连接错误求助
ETL流程报错排查与解决
问题背景
原本可正常运行的ETL流程,读取Google Sheets导出的CSV数据并插入PostgreSQL临时表,现在出现错误<urlopen error unknown url type: https>。
报错信息
main.ipynb db_session = database_handler.create_connection('config.json') data_handler.read_dataset_create_tables_and_insert_data(db_session) ERROR:root:An error occurred: <urlopen error unknown url type: https> ERROR:root:An error occurred: <urlopen error unknown url type: https> ...(重复报错省略) 2023-11-28 00:50:23.483310 - Failed to connect to database (database_handler.py) ##### <urlopen error unknown url type: https> 2023-11-28 00:50:23.485303 - Failed to connect to database (database_handler.py) ##### <urlopen error unknown url type: https>
核心代码片段
业务逻辑代码
def read_sheet_as_dataframe(sheet_info): file_type = sheet_info["type"] file_config_parameter = sheet_info["config"] sheet_name = sheet_info["sheet_name"] df = data_handler.read_data_as_dataframe(file_type, file_config_parameter) return df, sheet_name def read_dataset_create_tables_and_insert_data(db_session): sheets_info = [ {"type": lookups.FileType.CSV, "config": 'https://docs.google.com/spreadsheets/d/18GUCOh6BzZ6eLeM1fbLGrnPpW2DeUUal-jqci93w6R8/gviz/tq?tqx=out:csv&sheet=countries', "sheet_name": "countries"}, ...(其他sheet配置省略) ] for sheet_info in sheets_info: df, sheet_name = read_sheet_as_dataframe(sheet_info) create_table_and_insert_data(db_session, df, sheet_name)
config.json配置
{ "db_host" : "localhost", "db_name" : "Pluto", "db_user" : "postgres", "db_pass" : "Laptop2018", "port_id" : "5433", ...(其他数据源标识省略) }
数据库连接代码
def create_connection(config_file): db_session = None try: config_data = file_handler.read_config(config_file) if config_data is not None: db_host = config_data.get("db_host") db_name = config_data.get("db_name") db_user = config_data.get("db_user") db_pass = config_data.get("db_pass") db_port = config_data.get("port_id") if db_host and db_name and db_user and db_pass: db_session = psycopg2.connect( host=db_host, database=db_name, user=db_user, password=db_pass, port=db_port ) else: print("Missing database connection parameters in the config file.") else: print("Failed to read the configuration file.") except Exception as error: prefix = lookups.ErrorHandling.DB_CONNECTION_ERROR.value suffix = str(error) print_error_console(suffix, prefix) log_error(f'An error occurred: {str(error)}') finally: return db_session
排查方向与解决方案
1. 验证Python环境的HTTPS支持
unknown url type: https最常见原因是Python的urllib模块缺少SSL支持,可能是编译Python时未集成SSL库,或依赖库损坏。
- 运行以下命令验证SSL支持:
python -c "import ssl; print(ssl.OPENSSL_VERSION)" - 如果报错,说明缺少SSL支持:
- Linux系统:安装
openssl和libssl-dev,重新编译或安装Python。 - Windows系统:重新安装官方Python安装包(默认包含SSL支持)。
- Linux系统:安装
2. 检查Google Sheets导出URL的有效性与权限
- 直接在浏览器中打开报错的CSV URL,确认能正常下载CSV文件:
- 如果提示权限不足,将Google Sheet的共享设置改为任何人可查看。
- 检查URL中的
&sheet=countries部分,确认Sheet名称大小写、拼写与实际一致。
3. 缩小数据库连接代码的异常捕获范围
报错信息中同时出现数据库连接失败和URL错误,说明create_connection的except Exception捕获了非数据库连接的异常,干扰了问题定位:
- 修改
create_connection的异常捕获逻辑,只捕获PostgreSQL连接相关错误:except psycopg2.Error as error: # 替换原有的except Exception prefix = lookups.ErrorHandling.DB_CONNECTION_ERROR.value suffix = str(error) print_error_console(suffix, prefix) log_error(f'An error occurred: {str(error)}') - 这样其他模块(如读取CSV URL)的错误会在对应位置抛出,便于精准定位。
4. 检查CSV读取逻辑的HTTPS处理
如果data_handler.read_data_as_dataframe使用urllib或pandas读取URL,需确保HTTPS请求正常:
- 若使用
urllib.request,手动添加SSL上下文:import urllib.request import ssl import pandas as pd def read_data_as_dataframe(file_type, url): if file_type == lookups.FileType.CSV: ctx = ssl.create_default_context() # 若遇到证书问题可临时关闭验证(生产环境不建议) # ctx.check_hostname = False # ctx.verify_mode = ssl.CERT_NONE with urllib.request.urlopen(url, context=ctx) as response: df = pd.read_csv(response) return df - 若使用
pandas.read_csv,升级pandas到最新版本,避免旧版本的HTTPS兼容问题:pip install --upgrade pandas
内容的提问来源于stack exchange,提问作者Rami Chalouhi
相关产品推荐
相关产品推荐

