Id | 自动编号 | 标识,主键 |
oldID | 数字 | 旧数据编码 |
Title | 标题 | 文本 |
Content | 备注 | 内容 |
<%@LANGUAGE="JSCRIPT" CODEPAGE="936"%> <!-- METADATA NAME="Microsoft ActiveX Data Objects 2.5 Library" TYPE="TypeLib" UUID="{00000205-0000-0010-8000-00AA006D2EA4}" --> <% //打开数据库 try { var strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("#db.mdb"); var objConnection = Server.CreateObject("ADODB.Connection"); objConnection.Open(strConnectionString); } catch(e) { Response.Write(e.description); Response.End(); } %> <script language="jscript" runat="server"> //远程获取数据 function GetData() { var xHttp = new ActiveXObject("microsoft.xmlhttp"); xHttp.open("POST","http://www.chinamp.org/mppro2.php",false); xHttp.send(); return(xHttp.responseText); } //利用正则表达式提取符合条件的链接 function GetLinks(str) { var re = new RegExp("<a[^<>]+?\>((.|\n)*?)<\/a>", "gi"); var a = str.match(re); //第一次搜索 for(var i=0;i<a.length;i++) { var t1,t2; var temp; var r = /qy.php\?id=(\d+)/ig; if(!r.test(a[i]))continue; temp = a[i].match(/qy.php\?id=(\d+)/ig); t1 = RegExp.$1; temp = a[i].match(/<font[^<>]+?color=\"#000000\"\>(.*?)<\/font>/ig); t2 = RegExp.$1; if(t1 == t2)continue; SaveArticle(t1,t2,GetContent(t1)); } } //通过提取的链接获取ID,并通过这个ID取抓取相应的文章 function GetContent(id) { var xHttp = new ActiveXObject("microsoft.xmlhttp"); xHttp.open("POST","http://www.chinamp.org/qy.php?id=" + id,false); xHttp.send(); var str = xHttp.responseText; var re = new RegExp("<span[^<>]+?style=\"font-size:10\.8pt\">(.*?)<\/span>", "gi"); var a = str.match(re); return(RegExp.$1); } //入库 function SaveArticle(oldID,Title,Content) { var oRst = Server.CreateObject("ADODB.Recordset"); var sQuery; sQuery = "SELECT oldID,Title,Content FROM Articles" oRst.Open(sQuery,objConnection,adOpenStatic,adLockPessimistic); oRst.AddNew(); oRst("oldID") = oldID; oRst("Title") = Title; oRst("Content") = Content; oRst.Update(); oRst.Close(); Response.Write(Title + "抓取成功" + "<br>"); } </script> <HTML> <HEAD> <TITLE> 抓取文章</TITLE> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></HEAD> <BODY> <%=GetLinks(GetData())%> </BODY> </HTML> |