package com.cslg.ppa.service.GetWebArticle;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class GetEcigaretteService {
    private static final String LINK_SELECTOR = "a[href]";

    public void crawlEcigaretteArticles(String baseUrl) throws IOException {
        // 使用Jsoup连接并解析网页
        Document doc = Jsoup.connect(baseUrl)
                .timeout(15000) // 增加超时时间
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                .followRedirects(true) // 跟随重定向
                .get();

        List<String> list1 = new ArrayList<>();
        Elements elements = doc.select("div.app-container");
        Elements linkElements = elements.select(LINK_SELECTOR);
        for (Element element : linkElements) {
            String link = element.absUrl("href");
            list1.add(link);
        }
        Elements newsItems  = doc.select(LINK_SELECTOR);
        List<String> list = new ArrayList<>();
        for (Element item : newsItems) {
            try {
                Element linkElement = item.selectFirst(LINK_SELECTOR);
                String link = linkElement.absUrl("href"); // 获取绝对URL
                list.add(link);
            } catch (Exception e) {
                log.warn("解析单个新闻项时出错: ", e);
            }
        }
        System.out.println("-------------");
    }
}
