zoukankan      html  css  js  c++  java
  • jsoup解析页面

    package com.java.jsoup;
    /**
     * jsoup解析网页
    * @author nidegui
    * @version 2019年4月29日 下午5:12:02
    * 
    */
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class Demo1 {
    	public static void main(String[] args) throws ClientProtocolException, IOException {
    		
    		CloseableHttpClient httpClient = HttpClients.createDefault();
    		HttpGet httpget=new HttpGet("https://www.cnblogs.com/");
    		CloseableHttpResponse response = httpClient.execute(httpget);
    		HttpEntity entity = response.getEntity();
    		String content = EntityUtils.toString(entity);
    		
    		response.close();
    		Document doc = Jsoup.parse(content);
    		//css样式选择器
    		Elements aSelect = doc.select("#post_list .post_item .post_item_body h3 a");
    		for(Element a:aSelect) {
    			System.out.println("文本:"+a.text());
    			System.out.println("链接:"+a.attr("href"));
    		}
    		
    		
    	}
    }
    

      

  • 相关阅读:
    Leetcode#104 Maximum Depth of Binary Tree
    Leetcode#102 Binary Tree Level Order Traversal
    js 实时显示字数
    js获取链接参数
    DIV+CSS左右列高度自适应问题
    css 背景透明,文字不透明
    css position的值
    从头搭建vue项目
    vuejs怎么在服务器部署?
    windows下nginx安装、配置与使用
  • 原文地址:https://www.cnblogs.com/nidegui/p/10894739.html
Copyright © 2011-2022 走看看