日期:2014-05-17 浏览次数:20812 次
public void parse(String urlStr) { // 返回结果初始化。 Document doc = null; try { doc = Jsoup .connect(urlStr) .userAgent( "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)") // 设置User-Agent .timeout(5000) // 设置连接超时时间 .get(); } catch (MalformedURLException e) { log.error( e); return ; } catch (IOException e) { if (e instanceof SocketTimeoutException) { log.error( e); return ; } if(e instanceof UnknownHostException){ log.error(e); return ; } log.error( e); return ; } system.out.println(doc.title()); Element head = doc.head(); Elements metas = head.select("meta"); for (Element meta : metas) { String content = meta.attr("content"); if ("content-type".equalsIgnoreCase(meta.attr("http-equiv")) && !StringUtils.startsWith(content, "text/html")) { log.debug( urlStr); return ; } if ("description".equalsIgnoreCase(meta.attr("name"))) { system.out.println(meta.attr("content")); } } Element body = doc.body(); for (Element img : body.getElementsByTag("img")) { String imageUrl = img.attr("abs:src");//获得绝对路径 for (String suffix : IMAGE_TYPE_ARRAY) { if(imageUrl.indexOf("?")>0){ imageUrl=imageUrl.substring(0,imageUrl.indexOf("?")); } if (StringUtils.endsWithIgnoreCase(imageUrl, suffix)) { imgSrcs.add(imageUrl); break; } } } }
import java.io.BufferedReader; import java.io.FileReader; import org.cyberneko.html.parsers.DOMParser; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class Demo { public static String TextExtractor(Node root){ //若是文本节点的话,直接返回 if (root.getNodeType() == Node.TEXT_NODE) { return root.getNodeValue().trim(); } if(root.getNodeType() == Node.ELEMENT_NODE) { Element elmt = (Element) root; //抛弃脚本 if (elmt.getTagName().equals("STYLE") || elmt.getTagName().equals("SCRIPT")) return ""; NodeList children = elmt.getChildNodes(); StringBuilder text = new StringBuilder(); for (int i = 0; i < children.getLength(); i++) { text.append(TextExtractor(children.item(i))); } return text.toString(); } //对其它类型的节点,返回空值 return ""; } public static void main(String[] args) throws Exception{ //生成html parser DOMParser parser = new DOMParser(); //设置网页的默认编码 parser.setProperty( "http://cyberneko.org/html/properties/default-encoding ", "gb18030"); //input file BufferedReader in = new BufferedReader(new FileReader("input.htm")); parser.parse(new InputSource(in)); Document doc = parser.getDocument(); //获得body节点,以此为根,计算其文本内容 Node body = doc.getElementsByTagName("BODY").item(0); System.out.println(TextExtractor(body)); } }