(.*?)'
data_top = re.compile(pattern_top, re.S).findall(html)
data = data_top + data_rank
tail = (datetime.datetime.strftime(self.curTime,'%Y%m%d_%H%M%S'), interval_time)
for i in range(0,len(data)):
data[i] += tail
# print(data)
print('[success]爬取成功!')
return data
# 存入数据库
def saveDAO(self, data):
# data为 list 类型
# data[i]为 元组 类型,若len(data)为51,则data[0]为置顶
# (ranknum, searchItem, time, duration)
print('[start]开始写入数据库..........')
conn = mysql.connector.connect(host="127.0.0.1", port=3306, user="WeiboHotSearch",
password="ws1234.", database="WeiboHotSearch")
cursor = conn.cursor()
sql1="SELECT count(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='WeiboHotSearch' AND TABLE_NAME=%s;"
tablename= "table" + datetime.datetime.strftime(self.curTime,'%Y%m')
val1=(tablename,)
cursor.execute(sql1, val1)
res = cursor.fetchone()
if res[0]==0:
sql2="call CreateTablePro(%s);"
val2=(tablename,)
cursor.execute(sql2, val2)
for item in data:
sql3 = "call SelectDataPro(%s, %s, %s, %s, %s, @cnt);"
sql4 = "select @cnt;"
val3 = (tablename, int(item[0]), item[1], item[2], item[3])
cursor.execute(sql3, val3)
cursor.execute(sql4)
cnt = cursor.fetchone()
if cnt[0]==0:
sql4="call InsertDataPro(%s, %s, %s, %s, %s)"
val4=(tablename, int(item[0]), item[1], item[2], item[3])
cursor.execute(sql4, val4)
print("[Add ]新增条目:", item)
else:
sql5="call UpdateDataPro(%s, %s, %s, %s, %s)"
val5 = (tablename, int(item[0]), item[1], item[2], item[3])
cursor.execute(sql5, val5)
print("[Update]更新条目:", item)
conn.commit()
cursor.close()
conn.close()
def run(self):
print("[start] Time: ", datetime.datetime.strftime(self.curTime,'%Y-%m-%d %H:%M:%S'))
data = self.getData(self.getHtml())
self.saveDAO(data)
print("--------------------------------------------------------------")
print("[over]写入结束!")
print("--------------------------------------------------------------")
print("")
print("")
if __name__ == '__main__':
print("[Initialized] 开机自启动加载完成!等待运行!")
time.sleep(60) # 开机等待60秒再运行,防止数据库服务未启动等异常
while 1:
HotSearchThread().run()
time.sleep(interval_time * 60)
二、数据库实现
防止数据库单个表太大,按月份自动生成表,使用存储过程进行管理
存储过程:
--- CreateTablePro
CREATE DEFINER=`WeiboHotSearch`@`%` PROCEDURE `CreateTablePro`(IN `tableName` varchar(50))
BEGIN
set @tname = tableName;
set @sql_create_table = concat(
'CREATE TABLE IF NOT EXISTS ', @tname,
'(num int(2),searchItem varchar(255) primary key,hotTime varchar(255),duration int(5))'
);
PREPARE sql_create_table from @sql_create_table;
EXECUTE sql_create_table;
END
--- SelectDataPro
CREATE DEFINER=`WeiboHotSearch`@`%` PROCEDURE `SelectDataPro`(IN `tableName` varchar(50),IN `num` int(2),IN `searchItem` varchar(255),IN `hotTime` varchar(255),IN `duration` int(5), OUT `cnt` int(5))
BEGIN
set @tname = tableName;
set @num = num;
set @sItem = searchItem;
set @hTime = hotTime;
set @dur = duration;
set @sql_query_data = concat(
'select count(@sItem) into @cnt from ', @tname, ' where searchItem = @sItem'
);
PREPARE sql_query_data from @sql_query_data;
EXECUTE sql_query_data;
set cnt=@cnt;
END
--- InsertDataPro
CREATE DEFINER=`WeiboHotSearch`@`%` PROCEDURE `InsertDataPro`(IN `tableName` varchar(50),IN `num` int(2),IN `searchItem` varchar(255),IN `hotTime` varchar(255),IN `duration` int(5))
BEGIN
set @tname = tableName;
set @num = num;
set @sItem = searchItem;
set @hTime = hotTime;
set @dur = 0;
set @sql_insert_data = concat(
'insert into ', @tname,' values( @num, @sItem, @hTime, @dur);'
);
PREPARE sql_insert_data from @sql_insert_data;
EXECUTE sql_insert_data;
END
--- UpdateDataPro
CREATE DEFINER=`WeiboHotSearch`@`%` PROCEDURE `UpdateDataPro`(IN `tableName` varchar(50),IN `num` int(2),IN `searchItem` varchar(255),IN `hotTime` varchar(255),IN `duration` int(5))
BEGIN
set @tname = tableName;
set @num = num;
set @sItem = searchItem;
set @hTime = hotTime;
set @dur = duration;
set @sql_insert_data1 = concat(
'update ', @tname,' set duration=duration+@dur where searchItem = @sItem'
);
PREPARE sql_insert_data1 from @sql_insert_data1;
EXECUTE sql_insert_data1;
set @sql_insert_data = concat(
'update ', @tname,' set num=@num, hotTime=@hTime where searchItem = @sItem and num>@num'
);
PREPARE sql_insert_data from @sql_insert_data;
EXECUTE sql_insert_data;
END
其他 :
--- 建表模板
drop table tablename;
CREATE table tablename (
num int(2),
searchItem varchar(255) primary key,
hotTime varchar(255),
duration int(5)
)
--- 判断数据库中是否存在某张表
SELECT count(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='WeiboHotSearch' AND TABLE_NAME='test';
--- 存储过程的调用
call CreateTablePro('test')
call InsertDataPro('table202003', 0, '复工复产保卫战', '20200324_165840', 20)
call SelectDataPro('table202003', 0, '复工复产保卫战', '20200324_165840', 20, @cnt);
select @cnt;
call UpdateDataPro('table202003', 30, '方方', '20200324_165840', 20)
--- select 查询验证
select * from table202003 where duration = 20
--- mysql创建并调用含有out参数的存储过程
CREATE PROCEDURE sp_add(a int, b int,out c int)
begin
set c=a+ b;
end;
--- 调用过程:
call sp_add (1,2,@a);
select @a;
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000777c3290, pid=5632, tid=6656
#
# JRE version: Java(TM) SE Ru
Spring 中提供一些Aware相关de接口,BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,其中最常用到de匙ApplicationContextAware.实现ApplicationContextAwaredeBean,在Bean被初始后,将会被注入 Applicati
在Java项目中,我们通常会自己写一个DateUtil类,处理日期和字符串的转换,如下所示:
public class DateUtil01 {
private SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void format(Date d
问题描述:
在实现类中的某一或某几个Override方法发生编译错误如下:
Name clash: The method put(String) of type XXXServiceImpl has the same erasure as put(String) of type XXXService but does not override it
当去掉@Over