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

📄 parsecnkidetail.java

📁 本程序是专门用于从网页上自动收集cmi,cnki上的被引文献的数据
💻 JAVA
字号:
package cn.ac.cintcm.spider.cnki;

import org.htmlparser.Node;
import org.htmlparser.beans.FilterBean;
import org.htmlparser.tags.FrameTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.util.NodeList;


//import cn.ac.cintcm.spider.GetUrlContent;
import java.lang.NullPointerException;

public class ParseCnkiDetail {
	
	private static final int RETRY_COUNT = 3;

	public String getTitleDetail(String url){		
		String content=null;
		String refareFrameLink=null;
		refareFrameLink=getRefareFrameLink(url,"IFRAME");//引证文献对应的链接	
		if(refareFrameLink==null){
			LogEntity.logFile.log("没有得到引证文献对应的链接");	
			return content;
		}					
	    content=getRefareFrameContent(refareFrameLink,"P","style","line-height:150%");					
//		System.out.println(content);
		return content;
	}
	
//	得到引证文献对应的链接
	public String getRefareFrameLink(String url, String filterName) {
		String frameSrc=null;			    		
		GetUrlContent geturlContent=new GetUrlContent(url);	
		String resource=geturlContent.getContent();   //record的链接所对应页面的内容
		if(resource==null){
			LogEntity.logFile.log("网络忙,或者 "+url+" 不存在");
			return  frameSrc;
		}
		NodeList list = NodeFilters.getNodeList(resource,filterName,"name","refareframe1");
		if(list == null || list.size()==0){
			LogEntity.logFile.log("没有得到"+ url +" 对应的页面 ,或者 "+url+" 不存在");
			return frameSrc;
		}
		FrameTag frameTag = new FrameTag();				
		frameTag.setText(list.elementAt(0).toHtml());
		String srcString = frameTag.getAttribute("src");
		frameSrc="http://lsg.cnki.net/grid20/"+srcString;
		return frameSrc;
	}
	
	//得到引证文献的内容
	public String getRefareFrameContent(String url,String filterName,String attriName, String attriValue)throws NullPointerException{
		String contentAll="";
//		FilterBean bean=null;
//		for(int i=0;i<RETRY_COUNT;i++){
//			GetUrlContent geturlContent=new GetUrlContent(url);	
//			String resource=geturlContent.getContent(); 
//			if(resource==null){
//				continue;
//			}
//			bean=NodeFilters.getFilterBeans(resource,filterName,attriName,attriValue);
//			if(bean!=null)
//				break;
//		}	 
//		if(bean==null){
//			LogEntity.logFile.log("没有得到"+ url +" 对应的页面 ,或者 "+url+" 不存在");
//		}
//		contentAll=bean.getText().replaceAll("\r\n"," ").trim();
		
		GetUrlContent geturlContent=new GetUrlContent(url);	
		String resource=geturlContent.getContent(); 
		if(resource==null){
			LogEntity.logFile.log("网络忙,或者 "+url+" 不存在");
			return contentAll;
		}
	 		
		NodeList list=NodeFilters.getNodeList(resource,"TD","width","");
		if(list.size()==0 || list==null){
			LogEntity.logFile.log("没有得到"+ url +" 对应的页面 ,或者 "+url+" 不存在");
			return contentAll;
		}
		
		for(Node node:list.toNodeArray()){
		    int sign=0;
		    String records=null;
			for(Node node1:node.getChildren().toNodeArray()){
				if(node1.toString().indexOf("p style='line-height:150")>0) {
					if(sign==1){
						contentAll=contentAll + records;
						sign--;
					}
					NodeList tableNodes=node1.getChildren().extractAllNodesThatMatch(NodeFilters.getSingleFilter("TABLE"));
					records=tableNodes.elementAt(0).toPlainTextString().replaceAll("\r\n","");
//					records=node1.toPlainTextString().replaceAll("\r\n","").trim();
					sign++;
				}
				if(node1.toString().indexOf(">>更多")>0){
					sign--;
					LinkTag linkTag=new LinkTag();
					linkTag.setText(node1.getChildren().elementAt(0).toHtml());
					String  href=linkTag.getAttribute("href");
					String  moreurl="http://lsg.cnki.net/grid20"+href.substring(2);					
					contentAll=contentAll+getMoreRefareFrameContent(moreurl,"TD");
				}
				     				
			}
			if(sign==1){
				contentAll=contentAll + records;
			}
		}
		
		return contentAll;
	}
	
    public String getMoreRefareFrameContent(String url,String TagName){
    	String moreContent="";

		GetUrlContent geturlContent=new GetUrlContent(url);	
		String resource=geturlContent.getContent(); 
		if(resource==null){
			LogEntity.logFile.log("网络忙,或者 "+url+" 不存在");
			return moreContent;
		}
		
		NodeList list=NodeFilters.getNodeList(resource,TagName);
		if(list.size()==0 || list==null){
			LogEntity.logFile.log("没有得到"+ url +" 对应的页面 ,或者 "+url+" 不存在");
			return moreContent;
		}
		
		int pos = 1; // 标记source table column
		String result=null;
		for(Node node:list.toNodeArray()){
			TableColumn tag = new TableColumn();				
			tag.setText(node.toHtml());
			String bgcolor = tag.getAttribute("bgcolor");

			if (bgcolor != null
					&& (bgcolor.equals("#ffffff") || bgcolor
							.equals("#f1f7fe"))) {
				if(pos==1){
					result="["+node.toPlainTextString().replaceAll("&nbsp;&nbsp;&nbsp;&nbsp","").trim()+"]";
					pos++;
				}
				else if(pos==2||pos==3||pos==4){
					result=result+node.toPlainTextString().replaceAll("\n","").trim()+".";
					pos++;
				}
				else if(pos==5){
					moreContent=moreContent+result;
					pos=1;
					result=null;
				}
			}
		}   	
    	return moreContent;  	
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -