?? userinfoparser.java
字號:
package cn.myvideosite.exe.parser;
//import java.io.UnsupportedEncodingException;
import java.util.Date;
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.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import cn.myvideosite.commons.Constant;
import cn.myvideosite.data.model.bean.UserInfo;
import cn.myvideosite.data.model.services.UserinfoService;
import cn.myvideosite.util.HttpUtil;
import cn.myvideosite.util.MySuperDate;
public class UserInfoParser {
/**
* <div class="UserMsg"> http://www.56.com/h63/uv.index.php?user=haorui215
* 解析用戶信息
* @param url
*/
private static final NodeFilter FILTER_DIV_USERMSG=
new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class","UserMsg"));
/**
* <p>
* @param url
*/
private static final NodeFilter FILTER_DIV_p=new TagNameFilter("p");
/**
* <a
*/
private static final NodeFilter FILTER_A=new TagNameFilter("a");
/**
*
* @param url <span>
* @return
*/
private static final NodeFilter FILTER_span=new TagNameFilter("span");
/**
*
* @param url 導演(Director)信息
* @return <h2
*/
private static final NodeFilter FILTER_H2=new TagNameFilter("h2");
/**
*
* @param url <h3> 錯誤頁面<title>
* @return
*/
private static final NodeFilter FILTER_TITLE=new TagNameFilter("title");
public static UserInfo parse(String url){
UserInfo userinfo=UserinfoService.findByPerSpace(url); // 查找用戶地址 如果有就返回出來
if(userinfo!=null) return userinfo;
String page=HttpUtil.request(url, Constant.CHARSET_GB2312);
if(page !=null ){
if(page.equals("wfabc")){ return null;}
if(page.equals("頁面加載中,請一分鐘后<a href='javascript:window.location.reload();'>刷新</a>"))
{ return null;}
Parser pageParser=Parser.createParser(page, Constant.CHARSET_GB2312);
try {
NodeList title2NL=pageParser.parse(FILTER_TITLE);
if( title2NL !=null && title2NL.size()>0){
if(title2NL.elementAt(0) !=null ){
String str2=title2NL.elementAt(0).getChildren().elementAt(0).getText();
System.out.println(title2NL.elementAt(0));
//System.out.println(str2);
if(str2.equals("提示信息 - 56.com") || str2.equals("56?ㄦ??ㄥソ"))
{ return null; } // 56?ㄦ??ㄥソ 是utf-8格式
}
}
pageParser=Parser.createParser(page, Constant.CHARSET_GB2312); //判斷是否為用戶信息
NodeList h2NL=pageParser.parse(FILTER_H2);
if( h2NL !=null && h2NL.size()>0){
if(h2NL.elementAt(0) !=null ){
String str1=h2NL.elementAt(0).getChildren().elementAt(0).getText();
System.out.println(str1);
System.out.println("**********************");
if(str1.equals("用戶信息")) {
pageParser=Parser.createParser(page, Constant.CHARSET_GB2312);
NodeList nl=pageParser.parse(FILTER_DIV_USERMSG);
pageParser=Parser.createParser(nl.toHtml(), Constant.CHARSET_GB2312);
NodeList pNL=pageParser.parse(FILTER_DIV_p);
userinfo=new UserInfo();
if(pNL!=null && pNL.size()>0){
Node node = pNL.elementAt(0); //姓名
if(node !=null && !node.equals("")){
String str=node.toPlainTextString();
System.out.println("姓名:"+str.substring(str.indexOf(":")+1));
userinfo.setNickName(str.substring(str.indexOf(":")+1));
}
Node node2=pNL.elementAt(1); //性別
if(node2 !=null && !node2.equals("")){
String str=node2.toPlainTextString();
//System.out.println("性別:"+str.substring(str.indexOf(":")+1));
userinfo.setSex(str.substring(str.indexOf(":")+1));
}
Node node3=pNL.elementAt(2); //年齡 ?
if(node3 !=null && !node3.equals("")){
String str=node3.toPlainTextString();
//System.out.println("年齡:"+str.substring(str.indexOf(":")+1));
userinfo.setAge(Integer.parseInt(str.substring(str.indexOf(":")+1)));
}else
userinfo.setAge(18);
Node node4=pNL.elementAt(3); //所在地
if(node4 !=null && !node4.equals("")){
String str=node4.toPlainTextString();
//System.out.println("所在地:"+str.substring(str.indexOf(":")+1));
userinfo.setPlace(str.substring(str.indexOf(":")+1));
}else
userinfo.setPlace("");
Node node5=pNL.elementAt(4); //職業
if(node5 !=null && !node5.equals("")){
String str=node5.toPlainTextString();
//System.out.println("職業:"+str.substring(str.indexOf(":")+1));
userinfo.setVocation(str.substring(str.indexOf(":")+1));
}else
userinfo.setVocation("");
Node node6=pNL.elementAt(5); //最后登錄時間
if(node6 !=null && !node6.equals("")){
String str=node6.toPlainTextString();
System.out.println("最后登錄時間:"+str.substring(str.indexOf(":")+1));
userinfo.setLastTime(new MySuperDate(str.substring(str.indexOf(":")+1)).getDate());
}else
userinfo.setLastTime(new Date());
Node node7=pNL.elementAt(6);
if(node7 !=null && !node7.equals("")){
String str=node7.toPlainTextString();
//System.out.println("積分:"+str.substring(str.indexOf(":")+1)); //積分
userinfo.setScoreNub((Integer.parseInt(str.substring(str.indexOf(":")+1))));
}else
userinfo.setScoreNub(0);
Node node8=pNL.elementAt(7); //總人氣
if(node8 !=null && !node8.equals("")){
String str=node8.toPlainTextString();
//System.out.println("總人氣:"+str.substring(str.indexOf(":")+1).replace(" ? ","").trim());
userinfo.setTotalPop(Long.parseLong((str.substring(str.indexOf(":")+1).replace(" ? ","").trim())));
}else
userinfo.setTotalPop(0);
}
pageParser=Parser.createParser(page, Constant.CHARSET_GB2312); //個人空間地址
NodeList spanNL=pageParser.parse(FILTER_span);
if(spanNL !=null && spanNL.size()>0){
Node spanNode=spanNL.elementAt(0);
pageParser=Parser.createParser(spanNode.toHtml(), Constant.CHARSET_GB2312);
NodeList aNL=pageParser.parse(FILTER_A);
if(aNL !=null && aNL.size()>0){
LinkTag link=(LinkTag) aNL.elementAt(0);
System.out.println("個人空間地址:"+link.getLinkText());
System.out.println("*******************");
userinfo.setPersonalSpace(link.getLinkText());
}else
userinfo.setPersonalSpace("");
}
userinfo = UserinfoService.save(userinfo);
}else {
return null;
}
}
}
} catch (ParserException e) {
e.printStackTrace();
}
}
return userinfo;
}
public static void main(String[] args) {
// parser("http://www.56.com/h22/u_jason18188.html"); // http://www.56.com/h46/u_mayuan717.html
//http://www.56.com/h22/u_jason18188.html
//parser("http://dv.56.com/index.php?action=space&username=jiong1r1j&nick=%D2%BB%C8%D5%D2%BB%87%E5");
//出問題的
//parser("http://www.56.com/h87/u_wangweiduanpian.html");
// parser("http://www.56.com/h18/u_emedia.html"); //企業信息
// parser("http://www.56.com/h63/uv.index.php?user=haorui215");
// parser("http://www.56.com/h54/u_laikuanggen168.html");
//parse("http://www.56.com/h46/u_andylau.html"); //http://www.56.com/h46/u_andylau.html 頁面結束
// parse("http://www.56.com/h96/u_wangshuom4.html"); //空指針 http://www.56.com/h96/u_wangshuom4.html
//http://www.56.com/u36/v_MzYxNjI1Njk.html //空指針
/* try {
System.out.println(new String("56用戶您好".getBytes("GB2312"), "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
//http://www.56.com/h15/u_kobe2008920.html 空頁面
//parse("http://www.56.com/h15/u_kobe2008920.html");
//http://www.56.com/h12/u_lsw1365177.html
//parse("http://www.56.com/h19/u_jie86610052.html"); //http://zhiliangrs.v.56.com
parse("http://www.56.com/h95/u_sacullucas.html");//http://www.56.com/h48/u_mmj2007.html
//parse("http://www.56.com/h48/u_mmj2007.html");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -