import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.net.URL; /** * @author huQi * @email * @data 2021/2/2 16:13 */ public class MyJsoup { public static void main(String[] args) throws IOException { String url = "https://www.dongchedi.com/auto/library/x-x-x-x-x-x-x-x-x-x-x"; Document document = Jsoup.parse(new URL(url).openStream(), "utf-8", url); Elements elements = document.getElementsByClass("brands"); for(Element el:elements){ Document document1 = Jsoup.parse(el.toString()); Elements es = document1.getElementsByTag("P"); for(Element e:es){ System.out.println("品牌图片:"+e.getElementsByTag("img").attr("src")); System.out.println("品牌首字母:"+e.parent().getElementsByTag("h4").text()); System.out.println("品牌名称:"+e.text()); } } } }
maven坐标
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.13.1</version> </dependency>