数据问题?数据库获取,消息队列中,爬虫

    爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了)
    jsoup包
    1.包入依赖

    1. <!--解析网页-->
    2. <dependency>
    3. <groupId>org.jsoup</groupId>
    4. <artifactId>jsoup</artifactId>
    5. <version>1.10.2</version>
    6. </dependency>

    2.编写解析工具类

    1. @Component
    2. public class HtmlParseUtil {
    3. public static void main(String[] args) throws IOException {
    4. praseJD("java");
    5. }
    6. public static List<Content> praseJD(String keywords) throws IOException {
    7. //获取请求 https://search.jd.com/Search?keyword=java
    8. //前提,需要联网,
    9. String url = "https://search.jd.com/Search?keyword="+keywords;
    10. //解析网页(jsoup返回的Document就是浏览器的Document对象)
    11. Document document = Jsoup.parse(new URL(url), 3000);
    12. //所有在js中的方法这里都可以使用
    13. Element element = document.getElementById("J_goodsList");
    14. System.out.println(element.html());
    15. //获取所有的li元素
    16. Elements elements = element.getElementsByTag("li");
    17. ArrayList<Content> goodList = new ArrayList<>();
    18. //获取元素里面的内容
    19. for (Element el:elements){
    20. String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
    21. String price = el.getElementsByClass("p-price").eq(0).text();
    22. String title = el.getElementsByClass("p-name").eq(0).text();
    23. Content content = new Content();
    24. content.setTitle(title);
    25. content.setPrice(price);
    26. content.setImg(img);
    27. goodList.add(content);
    28. }
    29. return goodList;
    30. }
    31. }