由于測試環(huán)境需要一些測試數(shù)據(jù)或者壓測時需要大量的不重復的數(shù)據(jù)撮珠,可以使用SpringBoot定時任務 來抓取網(wǎng)站上 的證件號信息用來作為測試數(shù)據(jù)使用馏慨,本項目Github源碼
@Configuration
@EnableScheduling// 啟用定時任務
public class GetIdNos {
@Autowired
private UserService userService;
@Scheduled(cron = "0/5 * * * * ?")// 每10秒執(zhí)行一次 抓取網(wǎng)頁的身份證信息保存到數(shù)據(jù)庫
public void scheduler() throws Exception {
DateFormat dateFormat = DateFormat.getDateTimeInstance();
String time = dateFormat.format(new Date());
String url = "https://hk.51240.com/";
Document document = Jsoup.connect(url).get();
Elements trElements = document.getElementsByTag("table").get(3).getElementsByTag("tr");
for (int i = 1; i < trElements.size(); i++) {
Elements tds = trElements.get(i).getElementsByTag("td");
String name = tds.get(0).text();
String idNo = tds.get(1).text();
User user = userService.findUserByIdNo(idNo);
if (StringUtils.isEmpty(user)) {
userService.insert(new User(idNo, name, time));
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhongan</groupId>
<artifactId>GetPersonId</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
</dependencies>
</project>
成功爬取信息并保存