Jsoup解析线上html文件
/** * 根据url地址获取元素 * * @param url * 要爬虫的url地址 * @return 返回需要继续的元素值 * * 这里解析的为的 xy 网页上的应用信息 内容 ul 标签中 * */ public Elements getHtmlRescoureByurl(String url) { Connection con = Jsoup.connect(url).timeout(5000); try { con.header("Connection", "keep-alive"); con.header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36"); Document doc = con.get(); Elements divs = doc.getElementsByTag("div"); Element div = null; for (Element element : divs) { if (element.attr("class").equals("seventyfive")) { div = element; break; } } if (div != null) { Elements uls = div.getElementsByTag("ul"); if (uls.size() > 0) { Element ul = uls.get(0); return ul.getElementsByTag("li"); } } } catch (IOException e) { System.out.println(e.getMessage()); } return null; } /** * 解析返回的xml内容 * @param lis * @return */ public List<Object[]> parElement(Elements lis , int type) { List<Object[]> list = new ArrayList<Object[]>(); int i = 0; String defaulttime = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); for (Element li : lis) { String icon = li.getElementsByAttributeValue("class", "xy_img").attr("src"); // icon图标 Elements als = li.getElementsByTag("a"); String appname = als.get(2).text(); // appname String downloaded = li.getElementsByAttributeValue("class","edloaded").text(); // 下载次数 String app_size = li.getElementById("app_size_" + i).text(); String size = app_size.split("\\|").length > 0? app_size.split("\\|")[0]:""; // 软件大小 String updatetime = app_size.split("\\|").length > 1? app_size.split("\\|")[1]:defaulttime; // 更新日期 String itunesid = als.get(3).attr("id"); // itunesid String version = als.get(3).attr("version"); // 版本号 String bundleid = als.get(3).attr("bid"); // bundleid String ipaurl = als.get(3).attr("resurl"); ipaurl = getFromBase64(ipaurl); // ipa下载地址 i++; // ipa 下载地址为空 appname太长 更时间为空 // 参数顺序 itunesid,appname,icon,version,bundleid,size,downloaded,ipaurl,updatetime Object[] params = new Object[]{itunesid,appname,icon,version,bundleid,size,downloaded,type,ipaurl,updatetime}; list.add(params); } return list; }
需要jar jsoup
原文:http://www.cnblogs.com/zfy0098/p/5157338.html