java爬虫Deno
发布日期:2021-11-15 14:57:38 浏览次数:30 分类:技术文章

本文共 2205 字,大约阅读时间需要 7 分钟。

java爬虫的开发依赖于

直接上代码

public static void main(String[] args) {        HttpClient client = new DefaultHttpClient();        HttpGet httpGet = new HttpGet();        Map map = new HashMap();        try {            //百度百科999感冒灵连接            String url = "http://baike.baidu.com/link?url=c95Y4QJym_d_wFKGmcibRTI_KIyj-X_tOjnlOGJS9qekgO1tmWaWnrn7QyAjqvZX8At7LbI1XIa69IBZWejiCXDVM0jkSBMnVZKKu4jeg-ef4TJkKCXEXWcGJ8DRGTuHxW4qWB3pNNU7Y0KdrbNvGK";            // get请求获取页面信息            String bb = doget(url);            Document doc;            //用jsoup接收页面信息            doc = Jsoup.parse(bb);            // 选择所有div的class为para的标签            Elements news = doc.select("div[class=para]");            for (Element result : news) {                //获取标签的内容并打印                String str=result.text();                System.out.println(str);            }                                  } catch (Exception e) {            e.printStackTrace();        }     }      public static String doget(String path) {        InputStream is = null;        ByteArrayOutputStream baos = null;        try {            // 伪造referer 绕过防盗链设置            URL url = new URL(path.trim());            HttpURLConnection conn = (HttpURLConnection) url.openConnection();                          if (200 == conn.getResponseCode()) {                byte[] buff = new byte[4096];                int count;                ByteArrayOutputStream out = new ByteArrayOutputStream(4096);                InputStream in = conn.getInputStream();                 while ((count = in.read(buff)) != -1) {                    out.write(buff, 0, count);                }                conn.disconnect();                return out.toString("UTF-8");            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (baos != null) {                try {                    baos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    e.printStackTrace();                }            }         }         return null;    }

转载地址:https://blog.csdn.net/qq_39739458/article/details/80361370 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java8 新特性 Stream流 分组 排序 过滤 多条件去重
下一篇:mycat 入门教程

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月17日 15时52分30秒