首页>代码>java servlet调用百度AI开放平台动物识别API和百度百科API实现动物识别的实例>/Crawler/src/main/java/com/zooseefun/util/CrawlerUtil.java
package com.zooseefun.util; import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; import java.util.Scanner; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import com.alibaba.fastjson.JSONObject; public class CrawlerUtil { // public static final String // baseUrl="https://baike.baidu.com/search/word?word="; public static final String baseUrl = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key="; public static String lastUrl = "&bk_length=600"; // // // public String getAnimalInfo(String word) { // String introduction = getContent(baseUrl); // return introduction; // } // // // getContent()函数主要实现爬取输入文字的百度百科的介绍部分 // public static String getContent(String url) { // System.out.println(url); // // 利用URL解析网址 // URL urlObj = null; // try { // urlObj = new URL(url); // } catch (MalformedURLException e) { // System.out.println("The url was malformed!"); // return ""; // } // // URL连接 // URLConnection urlCon = null; // try { // urlCon = urlObj.openConnection(); // // 打开URL连接 // // 将HTML内容解析成UTF-8格式 // Document doc = Jsoup.parse(urlCon.getInputStream(), "utf-8", url); // // 刷选需要的网页内容 // String contentText = doc.select("div.lemma-summary").first().text(); // if(null==contentText){ // contentText = doc.select("div.parar").first().text(); // } // // 利用正则表达式去掉字符串中的"[数字]" // contentText = contentText.replaceAll("\\[\\d+\\]", ""); // return contentText; // } catch (IOException e) { // System.out.println("There was an error connecting to the URL"); // return ""; // } // } // public static List<String> queryBaike(String keyword) throws IOException // { // Document doc = Jsoup.connect( // "http://baike.baidu.com/search/none?word=" + keyword // + "&pn=0&rn=10&enc=utf8").get(); // String url = doc.select("a.result-title").first().attr("href"); // doc = Jsoup.connect(url).get(); // List<String> result = new ArrayList<String>(); // ListIterator<Element> paras = doc.select("div.lemma-summary") // .select("div.para").listIterator(); // String // img=doc.select("div.side-content").select("div.summary-pic").select("a").select("img").first().attr("src"); // while (paras.hasNext()) { // result.add(paras.next().text()); // } // result.add(img); // return result; // } public static String getAnimalInfo(String word) throws IOException { URL url = new URL(baseUrl + word + lastUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 读取服务器的数据 InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder builder = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) { builder.append(line); } String text = builder.toString(); JSONObject json = JSONObject.parseObject(text); json.put("image", json.get("image")); json.put("info", json.get("abstract")); json.put("desc", json.get("desc")); close(br); // 关闭数据流 close(is); // 关闭数据流 conn.disconnect(); // 断开连接 return json.toJSONString(); } protected static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } // public static void main(String[] args) throws IOException { // UnicodeUtil unicode = new UnicodeUtil(); // while (true) { // System.out.print("请输入:"); // Scanner in = new Scanner(System.in); // String word = in.next(); // System.out.println(baseUrl + word + lastUrl); // URL url = new URL(baseUrl + word + lastUrl); // HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // int statusCode = conn.getResponseCode(); // System.out.println("状态码:" + statusCode); // // 读取服务器的数据 // InputStream is = conn.getInputStream(); // BufferedReader br = new BufferedReader(new InputStreamReader(is)); // StringBuilder builder = new StringBuilder(); // String line = null; // while ((line = br.readLine()) != null) { // builder.append(line); // } // // String text = builder.toString(); // System.out.println("返回:" + text); // JSONObject json = JSONObject.parseObject(text); // System.out.println(json.toJSONString()); // close(br); // 关闭数据流 // close(is); // 关闭数据流 // conn.disconnect(); // 断开连接 // } // // } }

NoirMeow LV1
2024年5月21日
扫把多啦攻core LV1
2022年6月5日
ruishent LV6
2022年3月8日
helenli522 LV1
2021年5月3日
13261713015 LV4
2021年1月4日
guodaxia LV14
2020年8月31日
磊哥哥哥哥 LV13
2020年5月28日
linghongjun5002 LV10
2020年4月24日
wei112233 LV15
2020年4月21日
zq980707 LV6
2020年4月3日

啦啦啦7719 LV15
2024年10月15日
yuanmaxiazai LV2
2024年6月19日
EIFFELZY
2024年6月4日
暂无贡献等级
a1677596408 LV23
2024年4月26日
诺一啊啊啊啊啊 LV3
2024年4月7日
3334004690 LV10
2024年3月20日
1134116035
2024年2月25日
暂无贡献等级
tr1314qq LV17
2024年2月5日
微信网友_6761206459944960
2024年1月5日
暂无贡献等级
2017143155 LV12
2023年6月27日