爬虫

资料来源:B站狂神说

Java爬取网页

jar包

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.10.2</version>
</dependency>

代码

private List parse(String key) throws IOException {
    String url = "https://search.jd.com/Search?keyword=" + key + "&enc=utf-8&wq=java&pvid=c4b49ddc8c4543e48c2028e565c83a2d";
    Document document = Jsoup.parse(new URL(url), 30000);
    Element element = document.getElementById("J_goodsList");
    //System.out.println(element.html());
    Elements tags = element.getElementsByTag("li");
    List<Content> goods = new ArrayList<>();
    for (Element tag : tags) {
        String img = tag.getElementsByTag("img").eq(0).attr("data-lazy-img");
        String price = tag.getElementsByClass("p-price").eq(0).text();
        String name = tag.getElementsByClass("p-name").eq(0).text();
        goods.add(new Content(name, price, img));
    }
    return goods;
}
上一篇:线性规划


下一篇:jQuery获取select的值