private static void loadCSS(String address, HTMLDocument document) throws Exception { CSSData cssData = new CSSData(); document.putResource("CSS.DATA", cssData); NodePath nodePath = pathParser.toPath("HEAD"); HTMLNode head = extractor.lookNode(document.getRoot(), nodePath); URLUtils urlUtils = new URLUtils(); NodeIterator iterator = head.iterator(); while (iterator.hasNext()) { HTMLNode node = iterator.next(); if (!node.isNode(Name.LINK)) continue; Attributes attributes = node.getAttributes(); Attribute attribute = attributes.get("type"); if (attribute == null) continue; if (!"text/css".equalsIgnoreCase(attribute.getValue())) continue; attribute = attributes.get("href"); if (attribute == null) continue; String link = attribute.getValue(); if (link == null) continue; link = urlUtils.createURL(new URL(address), link); System.out.println(link); byte[] bytes = loadContent(link); String css = new String(bytes, "utf-8"); cssData.addValue(css); } }
public static void main(String[] args) throws Exception { String address = "http://vnexpress.net/GL/Xa-hoi/2009/02/3BA0B4AB/"; webClient.setURL(address, new URL(address)); // String address = "http://vnmedia.vn/newsdetail.asp?NewsId=154558&CatId=58"; java.net.URL url = new java.net.URL(address); HTMLDocument document = HTMLParser.createDocument(loadContent(address), "utf-8"); RefsDecoder decoder = new RefsDecoder(); NodeIterator iterator = document.getRoot().iterator(); while (iterator.hasNext()) { HTMLNode node = iterator.next(); if (!node.isNode(Name.CONTENT)) continue; char[] chars = node.getValue(); chars = decoder.decode(chars); chars = CharsUtil.cutAndTrim(chars, 0, chars.length); chars = java.text.Normalizer.normalize(new String(chars), Normalizer.Form.NFC).toCharArray(); node.setValue(chars); } loadCSS(address, document); NodePath nodePath = pathParser.toPath("BODY"); HTMLNode body = extractor.lookNode(document.getRoot(), nodePath); WebPageDataSearcher dataSearcher = new WebPageDataSearcher(document); HTMLNode node = dataSearcher.search(body); File file = new File("F:\\Temp2\\web\\output\\extract.htm"); byte[] bytes = new byte[0]; if (node != null) bytes = node.getTextValue().getBytes(Application.CHARSET); RWData.getInstance().save(file, bytes); }
boolean hasForm(HTMLNode node) { NodeIterator iterator = node.iterator(); while (iterator.hasNext()) { HTMLNode n = iterator.next(); if (n.isNode(Name.FORM)) return true; if (isFormElement(n)) return true; } return false; }
private boolean isLinkDiv(HTMLNode node) { if (node.getChildren() == null) return false; List<HTMLNode> ignores = new ArrayList<HTMLNode>(); NodeIterator iterator = node.iterator(); while (iterator.hasNext()) { HTMLNode n = iterator.next(); if (n.isNode(Name.A) && !linkNodeChecker.isValid(new CheckModel(n), 0)) ignores.add(n); } int counter = 0; iterator = node.iterator(ignores); while (iterator.hasNext()) { HTMLNode n = iterator.next(); if (n.isNode(Name.CONTENT)) { counter += countWord(n); } } return counter < 5 && ignores.size() > 1; }
protected void removeIFrameSource(HTMLNode node) { if (node.isNode(Name.IFRAME)) { Attributes attributes = node.getAttributes(); attributes.remove("src"); } List<HTMLNode> children = node.getChildren(); if (children == null || children.size() < 1) return; for (int i = 0; i < children.size(); i++) { removeIFrameSource(children.get(i)); } }
private int countWord(HTMLNode node) { if (node == null) return 0; NodeIterator nodeIterator = node.iterator(); int word = 0; while (nodeIterator.hasNext()) { HTMLNode iterNode = nodeIterator.next(); if (getAncestor(iterNode, Name.A, 0, 5) != null) continue; if (iterNode.isNode(Name.CONTENT)) { String text = iterNode.getTextValue(); word += textCounter.countWord(text, 0, text.length()); } } return word; }
private boolean isLinkContainer(HTMLNode node) { List<HTMLNode> children = node.getChildren(); if (children == null) return false; if (isListNode(children)) { NodeIterator nodeIterator = node.iterator(); int counter = 0; while (nodeIterator.hasNext()) { HTMLNode iterNode = nodeIterator.next(); if (iterNode.isNode(Name.A)) counter++; } return counter >= children.size() - 3; } for (int i = 0; i < children.size(); i++) { if (isLinkContainer(children.get(i))) return true; } return false; }
private HTMLNode getAncestor(HTMLNode node, Name name, int level, int max) { if (level > max || node == null) return null; if (node.isNode(name)) return node; return getAncestor(node.getParent(), name, level + 1, max); }