前言
最近研究了下python爬虫,想爬取一些数据存储到clickhouse里,进行分析。由于是新手,搜了好多教程,也踩了好几天的坑,记录一下,防止以后再走弯路。
连接代码
#引入库
from clickhouse_driver import Client
# 建立连接,注意这里的参数赋值方式
client = Client(host='xx.xx.xx.xx', port=8900, user='default', password='xxxxx@xxxxx', database='default')
result = client.execute('show tables')
print(type(result), result)
for row in result:
print(row)
client.disconnect()
经验总结
遇到的问题主要集中在建立连接语句上,client = Client(host=‘xx.xx.xx.xx’, port=8900, user=‘default’, password=‘xxxxx@xxxxx’, database=‘default’)
-
参数赋值方式。使用 host= 格式赋值
-
端口用错问题。clickhouse 数据库启动时会建立三个端口监听,http端口是给第三方工具连接使用的,tcp端口才是给python 代码连接使用的。要查看对应clickhouse配置的端口,参见其他文章