- ConnectionManager manager = Page.getConnectionManager();
- Parser parser = new Parser(manager
- .openConnection("http://www.verycd.com/topics/2760827/"));
- parser.setEncoding("GBK");
- //提取a标签里的img图片链接
- // NodeFilter filter = new AndFilter(new TagNameFilter("a"),
- // new HasChildFilter(new TagNameFilter("img")));
- // NodeFilter filter = new TagNameFilter("title");
- //提取input里面的东西
- NodeFilter filter = new AndFilter(new TagNameFilter("input"),
- new HasAttributeFilter("class","forminput"));
- // NodeList nodeList = parser.parse(filter);
- NodeList nodeList = parser.extractAllNodesThatMatch(filter);
- NodeIterator it = nodeList.elements();
- while (it.hasMoreNodes()) {
- Node node = it.nextNode();
- System.out.println(node.toHtml());
- }
- }
二、
- // 获取一个网页上所有的链接和图片链接
- public static void extracLinks(String url) {
- try {
- Parser parser = new Parser(url);
- parser.setEncoding("gb2312");
- //过滤 <frame> 标签的 filter,用来提取 frame 标签里的 src 属性所、表示的链接
- NodeFilter frameFilter = new NodeFilter() {
- public boolean accept(Node node) {
- if (node.getText().startsWith("frame src=")) {
- return true;
- } else {
- return false;
- }
- }
- };
- //OrFilter 来设置过滤 <a> 标签,<img> 标签和 <frame> 标签,三个标签是 or 的关系
- OrFilte rorFilter = new OrFilter(new NodeClassFilter(LinkTag.class), new
- NodeClassFilter(ImageTag.class));
- OrFilter linkFilter = new OrFilter(orFilter, frameFilter);
- //得到所有经过过滤的标签
- NodeList list = parser.extractAllNodesThatMatch(linkFilter);
- for (int i = 0; i < list.size(); i++) {
- Node tag = list.elementAt(i);
- if (tag instanceof LinkTag)//<a> 标签
- {
- LinkTag link = (LinkTag) tag;
- String linkUrl = link.getLink();//url
- String text = link.getLinkText();//链接文字
- System.out.println(linkUrl + "**********" + text);
- }
- else if (tag instanceof ImageTag)//<img> 标签
- {
- ImageTag image = (ImageTag) list.elementAt(i);
- System.out.print(image.getImageURL() + "********");//图片地址
- System.out.println(image.getText());//图片文字
- }
- else//<frame> 标签
- {
- //提取 frame 里 src 属性的链接如 <frame src="test.html"/>
- String frame = tag.getText();
- int start = frame.indexOf("src=");
- frame = frame.substring(start);
- int end = frame.indexOf(" ");
- if (end == -1)
- end = frame.indexOf(">");
- frame = frame.substring(5, end - 1);
- System.out.println(frame);
- }
- }
- } catch (ParserException e) {
- e.printStackTrace();
- }
- }
三、
- public void test2() throws ParserException{
- ConnectionManager manager = Page.getConnectionManager();
- Parser parser = new Parser(manager.openConnection("http://www.verycd.com/sto/datum/computer/page1"));
- //提取A标签,他有一个父标签为H3
- NodeFilter filter = new AndFilter(new TagNameFilter("a"),new HasParentFilter(new TagNameFilter("h3")));
- NodeList nodes = parser.parse(filter);
- NodeIterator it = nodes.elements();
- while (it.hasMoreNodes()) {
- Node node = it.nextNode(); //可以在此用debug查看node节点是什么类型的,然后在转换成具体类型来提取想要的数据!
- if (node instanceof LinkTag) { //转换
- LinkTag linkNode = (LinkTag) node;
- System.out.println("http://www.verycd.com"+linkNode.getAttribute("href"));
- }
- }
- }
四、
- ConnectionManager manager = Page.getConnectionManager();
- Parser parser = new Parser(manager.openConnection("http://huodong.sodao.com/39/info#"));
- NodeFilter filter = new StringFilter("减价幅度"); //用了StringFilter,可以立即提取出所在的text节点
- NodeList nodes = parser.parse(filter);
- NodeIterator it = nodes.elements();
- while (it.hasMoreNodes()) {
- Node node = it.nextNode(); //可以在此用debug查看node节点是什么类型的,然后在转换成具体类型来提取想要的数据!
- System.out.println(node.getParent().toHtml()); //通过indexof方法提取想要的text
五、
- /**
- * 用htmlparser提取图片的几种方法
- */
- public class Test {
- static Parser parser = new Parser();
- public static void test1(String url) throws ParserException{
- Parser parser = new Parser();
- parser.setURL(url);
- parser.setEncoding("GBK");
- //第一种方法
- NodeFilter imageFilter = new NodeFilter() {
- public boolean accept(Node node) {
- return (node instanceof ImageTag);
- }
- };
- NodeList images = parser.extractAllNodesThatMatch(imageFilter);
- // 第二种方法
- // NodeFilter imageFilter = new TagNameFilter("img");
- //第三种方法
- // NodeFilter imageFilter = new NodeClassFilter(ImageTag.class);
- // NodeList images = parser.parse(imageFilter);
- System.out.println("Size: " + images.size());
- for(NodeIterator it = images.elements(); it.hasMoreNodes();){
- ImageTag node = (ImageTag) it.nextNode();
- System.out.println(node.getAttribute("src"));
- }
- }
- public static void test2(String url) throws ParserException {
- Parser parser = new Parser();
- parser.setURL(url);
- parser.setEncoding("GBK");
- NodeFilter divFilter = new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("id","text_content"));
- NodeList divs = parser.parse(divFilter);
- System.out.println(removeTag(divs.elementAt(0).toHtml()));
- }
- public static String removeTag(String content) {
- if (null != content && !"".equals(content.trim())) {
- while (content.indexOf("<") >=0 && content.indexOf(">") >= 0) {
- int i = content.indexOf("<");
- int j = content.indexOf(">");
- if (i < j) {
- String contetn1 = content.substring(0, i);
- String content2 = content.substring(j + 1,
- content.length() );
- content = contetn1 + content2;
- }
- }
- }
- return content;
- }
- public static void main(String[] args) throws ParserException {
- String url = "http://news.dayoo.com/china/200908/11/53868_10386441.htm";
- Test.test2(url);
- }
- }
- =====================================================================
- /** *//**
- * 分别读纯文本和链接.
- * @param result 网页的内容
- * @throws Exception
- */
- public static void readTextAndLinkAndTitle(String result) throws Exception {
- Parser parser;
- NodeList nodelist;
- parser = Parser.createParser(result, "utf8");
- NodeFilter textFilter = new NodeClassFilter(TextNode.class);
- NodeFilter linkFilter = new NodeClassFilter(LinkTag.class);
- NodeFilter titleFilter = new NodeClassFilter(TitleTag.class);
- OrFilter lastFilter = new OrFilter();
- lastFilter.setPredicates(new NodeFilter[] { textFilter, linkFilter, titleFilter });
- nodelist = parser.parse(lastFilter);
- Node[] nodes = nodelist.toNodeArray();
- String line = "";
- for (int i = 0; i < nodes.length; i++) {
- Node node = nodes[i];
- if (node instanceof TextNode) {
- TextNode textnode = (TextNode) node;
- line = textnode.getText();
- } else if (node instanceof LinkTag) {
- LinkTag link = (LinkTag) node;
- line = link.getLink();
- } else if (node instanceof TitleTag) {
- TitleTag titlenode = (TitleTag) node;
- line = titlenode.getTitle();
- }
- if (isTrimEmpty(line))
- continue;
- System.out.println(line);
- }
- }
六、
- /**
- * @author rrong_m
- * @throws ParserException
- */
- public static void getWords(String url) throws ParserException {
- Parser parser = new Parser(url);
- NodeFilter filter = new HasAttributeFilter("id", "word_more_con");
- NodeList nodelist = parser.extractAllNodesThatMatch(filter);
- NodeFilter filter1 = new NodeClassFilter(LinkTag.class);
- nodelist = nodelist.extractAllNodesThatMatch(filter1, true);
- for (int i = 0; i < nodelist.size(); i++) {
- LinkTag link = (LinkTag) nodelist.elementAt(i);
- System.out.println(link.getLinkText() + ":" + link.getLink());
- }
- }
七、处理html里面的table
- public List getChangyu(String id) throws HttpException, IOException, ParserException
- {
- HttpClient hc=new HttpClient();
- hc.getParams().setContentCharset("gb2312");
- PostMethod pm=new PostMethod("http://61.145.121.47/custSearch.jsp");
- pm.setParameter("bnos","111111111111");
- hc.executeMethod(pm);
- String temp=pm.getResponseBodyAsString(50000);//设置获取html代码的数量,否则会报错,过大
- //System.out.print(temp);
- Parser parser=new Parser(temp);
- NodeFilter filter=new HasAttributeFilter("cellpadding","-1");
- NodeList nodelist=parser.extractAllNodesThatMatch(filter);
- if(nodelist.size()>0)
- {
- NodeFilter filter1=new NodeClassFilter(TableTag.class);
- nodelist=nodelist.extractAllNodesThatMatch(filter1,true);
- if(nodelist.size()>0)
- {
- TableTag table=(TableTag) nodelist.elementAt(0);
- TableRow [] rows=table.getRows();
- if(rows.length>1)
- {
- for(int i=2;i<rows.length;i++)
- {
- TableRow row=rows[i];
- TableColumn td=row.getColumns()[0];
- TableColumn td1=row.getColumns()[1];
- System.out.println(td.toPlainTextString()+" "+td1.toPlainTextString());
- }
- }
- }
- }
- else
- {
- }
- return null;
- }
八、查询IP
- public static void getIp(String ip) throws ParserException
- {
- String url="http://www.ip138.com/ips.asp?ip="+ip;
- Parser parser=new Parser(url);
- parser.setEncoding("gb2312");//设置编码否则得到的是乱码51jsp.cn
- NodeList nodelist=null;
- NodeFilter filter=new HasAttributeFilter("class","ul1");//定义过滤51jsp.cn
- nodelist=parser.extractAllNodesThatMatch(filter);
- if(nodelist.size()>0)
- {
- System.out.println("您查询的IP为:"+ip);
- System.out.println(nodelist.elementAt(0).toPlainTextString());
- }
- }
- public static void main(String[] args) throws ParserException {
- getIp("125.33.192.180");
- }
九、提取html里面的p标签里面的内容
- public static ArrayList <String> getParagraphList( String content){
- ArrayList <String> paraList=new ArrayList <String>();
- Parser myParser=new Parser();
- NodeList nodeList = null;
- NodeFilter paraFilter = new NodeClassFilter(ParagraphTag.class);
- try {
- myParser.setInputHTML(content);
- nodeList = myParser.parse(paraFilter);
- for (int i = 0; i <= nodeList.size(); i++) {
- ParagraphTag tag = (ParagraphTag) nodeList.elementAt(i);
- if(tag!=null){
- // System.out.println(tag.getStringText());
- // System.out.println("***********************************");
- paraList.add(tag.getStringText());
- }
- }
- } catch (ParserException e) {
- e.printStackTrace();
- }
- return paraList;
- }