从URL获取HTML来解析
Document doc = Jsoup.connect("http://www.baidu.com/").get();
String title = doc.title();
其中Jsoup.connect("xxx")方法返回一个org.jsoup.Connection对象。
在Connection对象中,我们可以执行get或者post来执行请求。但是在执行请求之前,
我们可以使用Connection对象来设置一些请求信息。比如:头信息,cookie,请求等待时间,代理等等来模拟浏览器的行为。
在Connection对象中,我们可以执行get或者post来执行请求。但是在执行请求之前,
我们可以使用Connection对象来设置一些请求信息。比如:头信息,cookie,请求等待时间,代理等等来模拟浏览器的行为。
Document doc = Jsoup.connect("http://example.com") .data("query", "Java") .userAgent("Mozilla") .cookie("auth", "token") .timeout(3000) .post();
获得Document对象后,接下来就是解析Document对象,并从中获取我们想要的元素了。
Document中提供了丰富的方法来获取指定元素。
◇使用DOM的方式来取得
getElementById(String id):通过id来获取
getElementsByTag(String tagName):通过标签名字来获取
getElementsByClass(String className):通过类名来获取
getElementsByAttribute(String key):通过属性名字来获取
getElementsByAttributeValue(String key, String value):通过指定的属性名字,属性值来获取
getAllElements():获取所有元素
◇通过类似于css或jQuery的选择器来查找元素
使用的是Element类的下记方法:
public Elements select(String cssQuery)
通过传入一个类似于CSS或jQuery的选择器字符串,来查找指定元素。
File input = new File("/tmp/input.html"); Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/"); Elements links = doc.select("a[href]"); //带有href属性的a元素 Elements pngs = doc.select("img[src$=.png]"); //扩展名为.png的图片 Element masthead = doc.select("div.masthead").first(); //class等于masthead的div标签 Elements resultLinks = doc.select("h3.r > a"); //在h3元素之后的a元素
<!-- 爬虫相关Jar包依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.11.3</version> </dependency>
String input = "毛巾"; // 需要爬取商品信息的网站地址 String url = "https://list.tmall.com/search_product.htm?q=" + input; // 动态模拟请求数据 CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // 模拟浏览器浏览(user-agent的值可以通过浏览器浏览,查看发出请求的头文件获取) httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"); CloseableHttpResponse response = httpclient.execute(httpGet); // 获取响应状态码 int statusCode = response.getStatusLine().getStatusCode(); try { HttpEntity entity = response.getEntity(); // 如果状态响应码为200,则获取html实体内容或者json文件 if(statusCode == 200){ String html = EntityUtils.toString(entity, Consts.UTF_8); // 提取HTML得到商品信息结果 Document doc = null; // doc获取整个页面的所有数据 doc = Jsoup.parse(html); //输出doc可以看到所获取到的页面源代码 // System.out.println(doc); // 通过浏览器查看商品页面的源代码,找到信息所在的div标签,再对其进行一步一步地解析 Elements ulList = doc.select("div[class='view grid-nosku']"); Elements liList = ulList.select("div[class='product']"); // 循环liList的数据(具体获取的数据值还得看doc的页面源代码来获取,可能稍有变动) for (Element item : liList) { // 商品ID String id = item.select("div[class='product']").select("p[class='productStatus']").select("span[class='ww-light ww-small m_wangwang J_WangWang']").attr("data-item"); System.out.println("商品ID:"+id); // 商品名称 String name = item.select("p[class='productTitle']").select("a").attr("title"); System.out.println("商品名称:"+name); // 商品价格 String price = item.select("p[class='productPrice']").select("em").attr("title"); System.out.println("商品价格:"+price); // 商品网址 String goodsUrl = item.select("p[class='productTitle']").select("a").attr("href"); System.out.println("商品网址:"+goodsUrl); // 商品图片网址 String imgUrl = item.select("div[class='productImg-wrap']").select("a").select("img").attr("data-ks-lazyload"); System.out.println("商品图片网址:"+imgUrl); System.out.println("------------------------------------"); } // 消耗掉实体 EntityUtils.consume(response.getEntity()); } else { // 消耗掉实体 EntityUtils.consume(response.getEntity()); } } finally { response.close(); }
https://www.jianshu.com/p/fd5caaaa950d
https://www.yiibai.com/jsoup/jsoup-quick-start.html