123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- """
- Mysql Functions
- """
- import pymysql
- class MysqlClient(object):
- """
- MySQL工具, env默认prod版本
- """
- def __init__(self):
- mysql_config = {
- # "host": "rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com",
- "host": "rr-bp1l12ea7e9wgu947.mysql.rds.aliyuncs.com",
- "port": 3306, # 端口号
- "user": "wx2016_longvideo", # mysql用户名
- "passwd": "wx2016_longvideoP@assword1234", # mysql用户登录密码
- "db": "longvideo", # 数据库名
- "charset": "utf8mb4" # 如果数据库里面的文本是utf8编码的,charset指定是utf8
- }
- self.connection = pymysql.connect(
- host=mysql_config['host'], # 数据库IP地址,内网地址
- port=mysql_config['port'], # 端口号
- user=mysql_config['user'], # mysql用户名
- passwd=mysql_config['passwd'], # mysql用户登录密码
- db=mysql_config['db'], # 数据库名
- charset=mysql_config['charset'] # 如果数据库里面的文本是utf8编码的,charset指定是utf8
- )
- def select(self, sql):
- """
- 查询
- :param sql:
- :return:
- """
- cursor = self.connection.cursor()
- cursor.execute(sql)
- data = cursor.fetchall()
- return data
- def update(self, sql):
- """
- 插入
- :param sql:
- :return:
- """
- cursor = self.connection.cursor()
- try:
- res = cursor.execute(sql)
- self.connection.commit()
- return res
- except Exception as e:
- self.connection.rollback()
- def close(self):
- """
- 关闭连接
- """
- self.connection.close()
- class MySQLClientSpider(object):
- """
- 爬虫的 mysql 配置
- """
- def __init__(self):
- mysql_config = {
- "host": "rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com", # 数据库IP地址,内网地址
- "port": 3306, # 端口号
- "user": "crawler", # mysql用户名
- "passwd": "crawler123456@", # mysql用户登录密码
- "db": "piaoquan-crawler", # 数据库名
- "charset": "utf8mb4" # 如果数据库里面的文本是utf8编码的,charset指定是utf8
- }
- self.connection = pymysql.connect(
- host=mysql_config['host'], # 数据库IP地址,内网地址
- port=mysql_config['port'], # 端口号
- user=mysql_config['user'], # mysql用户名
- passwd=mysql_config['passwd'], # mysql用户登录密码
- db=mysql_config['db'], # 数据库名
- charset=mysql_config['charset'] # 如果数据库里面的文本是utf8编码的,charset指定是utf8
- )
- def select(self, sql):
- """
- 查询
- :param sql:
- :return:
- """
- cursor = self.connection.cursor()
- cursor.execute(sql)
- data = cursor.fetchall()
- return data
- def close(self):
- """
- 关闭连接
- """
- self.connection.close()
- M = MySQLClientSpider()
- video_id = "14126697"
- sql = f"""SELECT like_cnt, play_cnt, duration from crawler_video where video_id = '{video_id}';"""
- w = M.select(sql)
- print(w[0])
- a, b, c = w[0]
- print(a, b, c)
|