Htmlparser is used to extract text content with a certain attribute of a specified tag. For example, div tags are extracted, and HD files with class attributes are extracted.
(1) filter class
As the name suggests, filter is used to filter the results and obtain the required content. Htmlparsert defines 16 different filters in the org.html parser. Filters package, which can be divided into several categories.
Filter of judgment class:
Tagnamefilter
Hasattributefilter
Haschildfilter
Hasparentfilter
Hassiblingfilter
Isequalfilter
Logical operation filter:
Andfilter
Notfilter
Orfilter
Xorfilter
Other filters:
Nodeclassfilter
Stringfilter
Linkstringfilter
Linkregexfilter
Regexfilter
Cssselectornodefilter
All filterclasses have the org.html parser. nodefilter interface. This interface has only one major function:
Boolean accept (node );
Each subclass implements this function to determine whether the input node meets the filter condition. If yes, true is returned. Otherwise, false is returned.
The following code tests the tagnamefilter class:
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.BodyTag;
import org.htmlparser.tags.JspTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.MetaTag;
import org.htmlparser.tags.TableTag;
import org.htmlparser.tags.TitleTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
public class t {
/**
* @param args
* @throws IOException
*/
public static Parser getParser(String url,String encoding) throws ParserException, IOException{
URL ur=new URL(url);
HttpURLConnection urlConnection=(HttpURLConnection) ur.openConnection();
Parser parser=new Parser(urlConnection);
parser.setEncoding(encoding);
return parser;
}
Public static void test1 (parser) throws parserexception {// obtain the content in the <body> </body> tag of the webpage and save it to the body.
Nodefilter bodyfilter = new nodeclassfilter (bodytag. Class );
Nodelist = parser. extractallnodesthatmatch (bodyfilter );
Node node = NULL;
For (INT I = 0; I <nodelist. Size (); I ++ ){
Node = nodelist. elementat (I );
String bodystring = (bodytag) node). getbody ();
System. Out. println (bodystring );
}
}
Public static void Test2 (parser) throws parserexception {// extract elements with certain attributes of a specific tag and obtain their text
Andfilter filter = new andfilter (New tagnamefilter ("Div"), new hasattributefilter ("class", "HD "));
Node node = NULL;
Nodelist = parser. parse (filter );
For (INT I = 0; I <nodelist. Size (); I ++ ){
// System. Out. println (1 );
Node = nodelist. elementat (I );
System. Out. println (node. toplaintextstring ());
}
}
Public static void main (string [] ARGs) throws ioexception, parserexception {
// Todo auto-generated method stub
// String content = readfile (testfilepath );
// String url = "http://www.sina.com /";
String url = "http://gs.dlut.edu.cn /";
String encoding = "UTF-8 ";
Parser = getparser (URL, encoding );
Test2 (parser );
}
}
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
public class tagNameFilter {
/**
* @param args
*/
public static Parser getParser(String url,String encoding) throws ParserException, IOException{
URL ur=new URL(url);
HttpURLConnection urlConnection=(HttpURLConnection) ur.openConnection();
Parser parser=new Parser(urlConnection);
parser.setEncoding(encoding);
return parser;
}
public static void test(Parser parser) throws ParserException{
NodeFilter filter=new TagNameFilter("span");
NodeList nodes=parser.extractAllNodesThatMatch(filter);
for(int i=0;i<nodes.size();i++){
Node node=nodes.elementAt(i);
System.out.println(node.toPlainTextString());
}
}
public static void main(String[] args) throws ParserException, IOException {
// TODO Auto-generated method stub
String url="http://gs.dlut.edu.cn/";
String encoding="utf-8";
Parser parser=getParser(url, encoding);
test(parser);
}
}