首页>代码>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;
}
}
最近浏览更多
ma406805131 LV19
2024年6月25日
微信网友_6902352269217792 LV3
2024年3月12日
adminLi LV2
2024年1月10日
yuan666 LV1
2023年12月4日
内心向阳 LV4
2023年11月7日
再曾一碗饭
2023年10月24日
暂无贡献等级
txf123 LV6
2023年9月21日
多加两块钱 LV4
2023年6月12日
GeminiLong LV6
2023年4月27日
fewfsdaf LV4
2023年4月16日

最近下载
