首页>代码>java通过jsoup爬取最代码牛币兑换活动>/spring-boot-jd-jsoup-master/src/main/java/com/simon/jsoup/job/PullPromotionJob.java
package com.simon.jsoup.job; import com.simon.jsoup.entity.Promotion; import com.simon.jsoup.utils.JSoupUtil; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.io.IOException; import java.util.List; /** * @author Simon */ @Component public class PullPromotionJob { @Value("${craw.site-url}") private String url; @Autowired RestTemplate restTemplate; /** * 每分钟爬取一次最代码活动页面信息 */ @Scheduled(cron = "0 0/1 * * * ? ") public void pullData() { ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class); if (entity.getStatusCode() == HttpStatus.OK) { String html = entity.getBody(); List<Promotion> promotions = JSoupUtil.parseHtml(html); if (promotions.isEmpty()) { return; } Promotion promotion = promotions.get(0); if (null == promotion) { return; } String name = promotion.getName(); String date = promotion.getPublishDate(); String info = promotion.getInfo(); String promotionStatus = promotion.getPromotionStatus(); if (StringUtils.isEmpty(name)) { return; } if (StringUtils.isEmpty(promotionStatus)) { return; } // if (promotionStatus.equals("已结束")) { // return; // } String url = "http://localhost:8080/jsoup/crawl?name=" + name + "&date=" + date + "&info=" + info + "&status=" + promotionStatus; Runtime runtime = Runtime.getRuntime(); try { runtime.exec("rundll32 url.dll,FileProtocolHandler " + url); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("爬取最代码活动信息失败:" + entity.getBody()); } } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }