Jsuop实现爬取数据

Jsuop实现爬取数据

Jsuop实现爬取数据

  • 首先引入依赖

    
            <dependency>
                <groupId>org.jsoupgroupId>
                <artifactId>jsoupartifactId>
                <version>1.10.2version>
            dependency>
    
  • 实现

    public List<Content> parseJD(String name) throws Exception {
            String url = "https://search.jd.com/Search?keyword="+name;
            //解析网页,返回值就是网页对象,30000是响应时间,随意设置
            Document document = Jsoup.parse(new URL(url), 30000);
            //参数为需要获取内容的class
            Element goodsList = document.getElementById("J_goodsList");
            Elements elements = goodsList.getElementsByTag("li");
            ArrayList<Content> contents = new ArrayList<>();
            for (Element element : elements) {
                String img = element.getElementsByTag("img").eq(0).attr("src");
                String price = element.getElementsByClass("p-price").eq(0).text();
                String title = element.getElementsByClass("p-name").eq(0).text();
    
                Content content = new Content(title, price, img);
                contents.add(content);
            }
            return contents;
        }
    

你可能感兴趣的:(java)