首页>代码>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;
    }
}
最近下载更多
maojiujiu  LV3 2023年2月7日
SZEPEZS  LV8 2022年6月10日
开发中  LV5 2022年4月8日
最代码官方  LV167 2022年3月3日
最近浏览更多
adminLi  LV1 1月10日
yuan666  LV1 2023年12月4日
内心向阳  LV4 2023年11月7日
再曾一碗饭 2023年10月24日
暂无贡献等级
txf123  LV4 2023年9月21日
多加两块钱  LV4 2023年6月12日
GeminiLong  LV6 2023年4月27日
fewfsdaf  LV4 2023年4月16日
神龙摆尾无拘束  LV2 2023年3月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友