企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 1. 错误 ### 1.1 'latin-1' codec can't encode character '\u5e74' in position 4: ordinal not in range(256) 这种情况是连接mysql的时候没有指定连接使用的字符集utf-8 解决办法:这样连接 ~~~ mysqlcli = pymysql.connect(host='192.168.56.130', user='root', passwd='tuna', db='crawl_data', port=3306, charset="utf8", use_unicode=True) ~~~ ## 2. 连接mysql操作 ~~~ class MysqlUtil: def __init__(self): self.__conn = pymysql.connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASSWORD,MYSQL_DB,charset="utf8", use_unicode=True) self.__cursor = self.__conn.cursor() @classmethod def getConnection(cls): return cls() @property def conn(self): return self.__conn def getProxyIpFromMysql(self): sql = """ select ip,port,ip_type,ip_position from {0} WHERE ip_type = 'HTTPS' order by rand() LIMIT 1 """.format(MYSQL_VALIDATED_TABLE) self.__cursor.execute(sql) return self.__cursor.fetchone() def validateIp(self,protocol="http",position=''): res = self.getProxyIpFromMysql() for result in res: ip = result[0] port = result[1] protocol = result[2] position = result[3] proxy = protocol.lower() + "://" + ip + ":" + port status = self.ipJudge.judgeMent(self.targetUrl,proxy,2) if status : print("ip:{0},port:{1},位置:{2}可以使用".format(ip,port,position)) # self.inserValidIp(ip,port,protocol,position) else: print("ip:{0},port:{1},位置:{2}不可以使用,进行删除".format(ip, port, position)) self.deleteIp(ip) ~~~