Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
searchhotcrawler
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhiwei
searchhotcrawler
Commits
c267bf23
Commit
c267bf23
authored
Nov 13, 2020
by
chenweitao
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'mlbWork' into 'master'
redis连接设置 See merge request
!55
parents
cc442545
3995d426
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
21 deletions
+67
-21
src/main/java/com/zhiwei/searchhotcrawler/crawler/WeiboHotSearchCrawler.java
+2
-3
src/main/java/com/zhiwei/searchhotcrawler/dao/RedisDao.java
+63
-16
src/main/java/com/zhiwei/searchhotcrawler/timer/quartz/GatherTimer.java
+2
-2
No files found.
src/main/java/com/zhiwei/searchhotcrawler/crawler/WeiboHotSearchCrawler.java
View file @
c267bf23
...
@@ -41,6 +41,8 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -41,6 +41,8 @@ import org.springframework.beans.factory.annotation.Autowired;
public
class
WeiboHotSearchCrawler
{
public
class
WeiboHotSearchCrawler
{
private
static
HttpBoot
httpBoot
=
new
HttpBoot
.
Builder
().
retryTimes
(
3
).
build
();
private
static
HttpBoot
httpBoot
=
new
HttpBoot
.
Builder
().
retryTimes
(
3
).
build
();
private
static
RedisDao
redisDao
=
new
RedisDao
();
/**
/**
* @Title: weiboHotSearchTest
* @Title: weiboHotSearchTest
* @author hero
* @author hero
...
@@ -112,7 +114,6 @@ public class WeiboHotSearchCrawler {
...
@@ -112,7 +114,6 @@ public class WeiboHotSearchCrawler {
* @return void 返回类型
* @return void 返回类型
*/
*/
public
static
List
<
HotSearchList
>
weiboHotSearchByPhone
(
Date
date
){
public
static
List
<
HotSearchList
>
weiboHotSearchByPhone
(
Date
date
){
RedisDao
redisDao
=
new
RedisDao
();
String
url
=
"https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=pos%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30&luicode=10000011&lfid=231583"
;
String
url
=
"https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=pos%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30&luicode=10000011&lfid=231583"
;
Map
<
String
,
String
>
headerMap
=
new
HashMap
<>();
Map
<
String
,
String
>
headerMap
=
new
HashMap
<>();
headerMap
.
put
(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
);
headerMap
.
put
(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
);
...
@@ -168,7 +169,6 @@ public class WeiboHotSearchCrawler {
...
@@ -168,7 +169,6 @@ public class WeiboHotSearchCrawler {
continue
;
continue
;
}
}
// }
// }
redisDao
.
closeRedis
();
return
result
;
return
result
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
"解析微博时热搜时出现解析错误,数据不是json结构"
,
e
);
log
.
error
(
"解析微博时热搜时出现解析错误,数据不是json结构"
,
e
);
...
@@ -177,7 +177,6 @@ public class WeiboHotSearchCrawler {
...
@@ -177,7 +177,6 @@ public class WeiboHotSearchCrawler {
log
.
info
(
"解析微博时热搜时出现解析错误,页面结构有问题"
);
log
.
info
(
"解析微博时热搜时出现解析错误,页面结构有问题"
);
}
}
}
}
redisDao
.
closeRedis
();
return
Collections
.
emptyList
();
return
Collections
.
emptyList
();
}
}
...
...
src/main/java/com/zhiwei/searchhotcrawler/dao/RedisDao.java
View file @
c267bf23
...
@@ -6,6 +6,7 @@ import redis.clients.jedis.Jedis;
...
@@ -6,6 +6,7 @@ import redis.clients.jedis.Jedis;
import
redis.clients.jedis.JedisPool
;
import
redis.clients.jedis.JedisPool
;
import
redis.clients.jedis.JedisPoolConfig
;
import
redis.clients.jedis.JedisPoolConfig
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.Set
;
/**
/**
...
@@ -14,16 +15,15 @@ import java.util.Set;
...
@@ -14,16 +15,15 @@ import java.util.Set;
@Log4j2
@Log4j2
public
class
RedisDao
{
public
class
RedisDao
{
private
Jedis
jedis
;
private
Jedis
Pool
jedisPool
;
public
RedisDao
(){
public
RedisDao
(){
JedisPoolConfig
poolConfig
=
new
JedisPoolConfig
();
JedisPoolConfig
poolConfig
=
new
JedisPoolConfig
();
poolConfig
.
setMaxIdle
(
RedisConfig
.
redisMaxIdle
);
poolConfig
.
setMaxIdle
(
RedisConfig
.
redisMaxIdle
);
poolConfig
.
setMaxTotal
(
RedisConfig
.
redisMaxTotal
);
poolConfig
.
setMaxTotal
(
RedisConfig
.
redisMaxTotal
);
poolConfig
.
setMinIdle
(
RedisConfig
.
redisMinIdle
);
poolConfig
.
setMinIdle
(
RedisConfig
.
redisMinIdle
);
JedisPool
jedisPool
=
new
JedisPool
(
poolConfig
,
RedisConfig
.
redisHost
,
RedisConfig
.
redisPort
,
RedisConfig
.
redisTimeout
);
poolConfig
.
setMaxWaitMillis
(
RedisConfig
.
redisTimeout
);
jedis
=
jedisPool
.
getResource
();
jedisPool
=
new
JedisPool
(
poolConfig
,
RedisConfig
.
redisHost
,
RedisConfig
.
redisPort
);
jedis
.
select
(
RedisConfig
.
redisDataBase
);
}
}
/**
/**
...
@@ -32,7 +32,17 @@ public class RedisDao {
...
@@ -32,7 +32,17 @@ public class RedisDao {
* @param value
* @param value
*/
*/
public
void
setRedisData
(
String
key
,
String
value
){
public
void
setRedisData
(
String
key
,
String
value
){
jedis
.
set
(
key
,
value
);
Jedis
jedis
=
jedisPool
.
getResource
();
jedis
.
select
(
RedisConfig
.
redisDataBase
);
try
{
jedis
.
set
(
key
,
value
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
finally
{
if
(
jedis
!=
null
){
jedis
.
close
();
}
}
}
}
/**
/**
...
@@ -41,7 +51,19 @@ public class RedisDao {
...
@@ -41,7 +51,19 @@ public class RedisDao {
* @return
* @return
*/
*/
public
String
getRedisData
(
String
key
){
public
String
getRedisData
(
String
key
){
return
jedis
.
get
(
key
);
Jedis
jedis
=
jedisPool
.
getResource
();
jedis
.
select
(
RedisConfig
.
redisDataBase
);
String
s
=
null
;
try
{
s
=
jedis
.
get
(
key
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
finally
{
if
(
jedis
!=
null
){
jedis
.
close
();
}
}
return
s
;
}
}
/**
/**
...
@@ -50,7 +72,17 @@ public class RedisDao {
...
@@ -50,7 +72,17 @@ public class RedisDao {
* @param value
* @param value
*/
*/
public
void
addDataToSet
(
String
key
,
String
value
){
public
void
addDataToSet
(
String
key
,
String
value
){
jedis
.
sadd
(
key
,
value
);
Jedis
jedis
=
jedisPool
.
getResource
();
jedis
.
select
(
RedisConfig
.
redisDataBase
);
try
{
jedis
.
sadd
(
key
,
value
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
finally
{
if
(
jedis
!=
null
){
jedis
.
close
();
}
}
}
}
/**
/**
...
@@ -59,7 +91,19 @@ public class RedisDao {
...
@@ -59,7 +91,19 @@ public class RedisDao {
* @return
* @return
*/
*/
public
Set
<
String
>
getRedisSetData
(
String
key
){
public
Set
<
String
>
getRedisSetData
(
String
key
){
return
jedis
.
smembers
(
key
);
Jedis
jedis
=
jedisPool
.
getResource
();
jedis
.
select
(
RedisConfig
.
redisDataBase
);
Set
<
String
>
set
=
new
HashSet
<>();
try
{
set
=
jedis
.
smembers
(
key
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
finally
{
if
(
jedis
!=
null
){
jedis
.
close
();
}
}
return
set
;
}
}
/**
/**
...
@@ -67,13 +111,16 @@ public class RedisDao {
...
@@ -67,13 +111,16 @@ public class RedisDao {
* @param key
* @param key
*/
*/
public
void
removeRedis
(
String
key
){
public
void
removeRedis
(
String
key
){
jedis
.
del
(
key
);
Jedis
jedis
=
jedisPool
.
getResource
();
}
jedis
.
select
(
RedisConfig
.
redisDataBase
);
try
{
/**
jedis
.
del
(
key
);
* 关闭redis连接
}
catch
(
Exception
e
){
*/
e
.
printStackTrace
();
public
void
closeRedis
(){
}
finally
{
jedis
.
close
();
if
(
jedis
!=
null
){
jedis
.
close
();
}
}
}
}
}
}
src/main/java/com/zhiwei/searchhotcrawler/timer/quartz/GatherTimer.java
View file @
c267bf23
...
@@ -29,6 +29,8 @@ import java.util.*;
...
@@ -29,6 +29,8 @@ import java.util.*;
public
class
GatherTimer
{
public
class
GatherTimer
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
GatherTimer
.
class
);
private
Logger
logger
=
LoggerFactory
.
getLogger
(
GatherTimer
.
class
);
private
RedisDao
redisDao
=
new
RedisDao
();
/** 知乎数码子分类 */
/** 知乎数码子分类 */
private
String
DIGITAL
=
"digital"
;
private
String
DIGITAL
=
"digital"
;
/** 知乎国际子分类 */
/** 知乎国际子分类 */
...
@@ -57,7 +59,6 @@ public class GatherTimer {
...
@@ -57,7 +59,6 @@ public class GatherTimer {
@Scheduled
(
cron
=
"45 0/10 * * * ? "
)
@Scheduled
(
cron
=
"45 0/10 * * * ? "
)
public
void
updateWeiBo
(){
public
void
updateWeiBo
(){
logger
.
info
(
"微博热搜导语更新..."
);
logger
.
info
(
"微博热搜导语更新..."
);
RedisDao
redisDao
=
new
RedisDao
();
HotSearchCacheDAO
hotSearchCacheDAO
=
new
HotSearchCacheDAO
();
HotSearchCacheDAO
hotSearchCacheDAO
=
new
HotSearchCacheDAO
();
Set
<
String
>
hotSearchIdSet
=
redisDao
.
getRedisSetData
(
RedisConfig
.
WEIBO_HOTSEARCHIDS
);
Set
<
String
>
hotSearchIdSet
=
redisDao
.
getRedisSetData
(
RedisConfig
.
WEIBO_HOTSEARCHIDS
);
redisDao
.
removeRedis
(
RedisConfig
.
WEIBO_HOTSEARCHIDS
);
redisDao
.
removeRedis
(
RedisConfig
.
WEIBO_HOTSEARCHIDS
);
...
@@ -73,7 +74,6 @@ public class GatherTimer {
...
@@ -73,7 +74,6 @@ public class GatherTimer {
ZhiWeiTools
.
sleep
(
3000L
);
ZhiWeiTools
.
sleep
(
3000L
);
}
}
}
}
redisDao
.
closeRedis
();
logger
.
info
(
"微博热搜导语更新结束..."
);
logger
.
info
(
"微博热搜导语更新结束..."
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment