⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userinfoparser.java

📁 模拟的土豆网视频网站
💻 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 + -