protected void bntInterfaceAdd_Click(object sender, EventArgs e) { if (ddlInterfaceType.SelectedItem.Value == "-1") { ddlInterfaceType.Focus(); return; } if (txtInterfaceUrl.Text.Equals("") ||txtInterfaceName.Text.Equals("") ||txtInterfaceBody.Text.Equals("") ||txtInterfaceSource.Text.Equals("") ||txtInterfaceSourceUrl.Text.Equals("")) { return; } CnInterfaceInfo cnf = new CnInterfaceInfo(); cnf.CnInterfaceTypeID = Convert.ToInt32(ddlInterfaceType.SelectedItem.Value); cnf.CnInterfaceAppearTime = Convert.ToDateTime(txtInterfaceAppearTime.Text); cnf.CnInterfaceName = txtInterfaceName.Text; cnf.CnInterfaceUrl = txtInterfaceUrl.Text; cnf.CnInterfacebody = txtInterfaceBody.Text; cnf.CnInterfaceSource = txtInterfaceSource.Text; cnf.CnInterfaceSourceUrl = txtInterfaceSourceUrl.Text; InterfaceInfo IFI = new InterfaceInfo(); int insertcount = IFI.InsertCnInterfaceInfo(cnf); Response.Write(insertcount.ToString()); }
/// <summary> /// 获取有道接口查询结果 /// </summary> /// <param name="html">网络地址</param> /// <returns>有道查询结果</returns> public static int GetCnInterfaceInfoFromYoudao(string url) { int insertcount = 0; WebClient wc = new WebClient { Encoding = Encoding.GetEncoding("gb2312") }; string html = wc.DownloadString(url.Trim()); //string html = ""; //System.IO.StreamReader sr = new System.IO.StreamReader("D:\\CnInterface\\CnWeb\\html\\youdaocode.txt", Encoding.GetEncoding("gb2312")); //lock (sr) //{ // html = sr.ReadToEnd(); // sr.Close(); //} var doc = new HtmlDocument(); doc.LoadHtml(html); string nameValue = ""; string urlValue = ""; string bodyValue = ""; string timeValue = ""; //StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 10; i++) { try { string nameXpath = "/body[1]/div[6]/div[2]/ul[1]/li["+i.ToString()+"]/h3[1]/a[1]"; string urlXpath = "/body[1]/div[6]/div[2]/ul[1]/li[" + i.ToString() + "]/h3[1]/a[1]/@href[1]"; string bodyXpath = "/body[1]/div[6]/div[2]/ul[1]/li[" + i.ToString() + "]/div[1]"; string timeXpath = "/body[1]/div[6]/div[2]/ul[1]/li[" + i.ToString() + "]/p[1]/span[1]"; nameValue = doc.DocumentNode.SelectSingleNode(nameXpath).InnerText; nameValue = new HtmlToText().ConvertHtml(nameValue).Trim(); urlValue = doc.DocumentNode.SelectSingleNode(urlXpath).Attributes["href"].Value; try { bodyValue = doc.DocumentNode.SelectSingleNode(bodyXpath).InnerHtml; bodyValue = new HtmlToText().ConvertHtml(bodyValue).Trim(); } catch { bodyValue = ""; } try { timeValue = doc.DocumentNode.SelectSingleNode(timeXpath).InnerText; timeValue = timeValue.Substring(timeValue.LastIndexOf("20")).Replace("-->", ""); } catch { timeValue = ""; } //sb.Append(" <div style=\"margin-top: 20px; margin-bottom: 20px; width:678px\">"); //sb.Append(" <div>"); //sb.Append(" <a target=\"_blank\" href=\"" + urlValue + "\"><font color=blue>" + nameValue.Replace("接口", "<font color=red>接口</font>") + "</font></a>"); //sb.Append(" </div>"); //sb.Append(" <div style=\"font-size: 13px;\">"); //sb.Append(" <font size=2 >" + bodyValue.Replace("接口", "<font color=red>接口</font>") + "</font>"); //sb.Append(" </div>"); //sb.Append(" <div>"); //sb.Append(" <font color=#006600>" + urlValue + "</font> <font size=2 color=#006600>" + timeValue + "</font> <a target=\"_blank\" href=\"" + url + "\"><font size=2 color=#666699>有道搜索</font></a>"); //sb.Append(" </div>"); //sb.Append(" </div>"); } catch { continue; } try { DateTime dt = Convert.ToDateTime(timeValue); } catch { timeValue = DateTime.Now.ToString("yyyy-MM-dd"); } CnInterfaceInfo cnf = new CnInterfaceInfo { CnInterfaceTypeID = 2, CnInterfaceName = nameValue, CnInterfacebody = bodyValue, CnInterfaceUrl = urlValue, CnInterfaceSource = "有道搜索", CnInterfaceSourceUrl = url, CnInterfaceAppearTime = Convert.ToDateTime(timeValue) }; InterfaceInfo IFI = new InterfaceInfo(); int cont = IFI.InsertCnInterfaceInfo(cnf); if (cont > 0) insertcount++; System.Threading.Thread.Sleep(200); } return insertcount; }
/// <summary> /// 通过URL获取接口对象. /// </summary> /// <param name="wc">WebClient 对象.</param> /// <param name="url">接口列表URL.</param> /// <param name="cnurlxp">接口地址(XPath).</param> /// <param name="cnurlRgx">接口地址正则表达式(Regex).</param> /// <param name="cnnamexp">接口名称(XPath).</param> /// <param name="cnnameRgx">接口名称正则表达式(Regex)</param> /// <param name="cnbodyXp">接口内容(XPath).</param> /// <param name="cnbodyRgx">接口内容正则表达式(Regex).</param> /// <param name="cntimeXp">接口发布时间(XPath).</param> /// <param name="cntimeRgx">接口发布时间正则表达式(Regex).</param> /// <param name="cntimeF">接口发布时间Format.</param> /// <param name="cnsource">接口网络来源.</param> /// <param name="cnsourceurl">接口网络来源地址.</param> /// <returns>接口对象.</returns> public static int GetCnInterfaceInfoEntity(WebClient wc, string url, string cnurlxp, string cnurlRgx, string cnnamexp, string cnnameRgx, string cnbodyXp, string cnbodyRgx, string cntimeXp, string cntimeRgx, string cntimeF, string cnsource, string cnsourceurl, int cntypeid, int sourceno, int pagesize) { var html = wc.DownloadString(url); var doc = new HtmlDocument(); doc.LoadHtml(html); int insertcount = 0; for (int i = 1; i <=pagesize; i++) { try { //xpath处理 string urlllog = ""; if (sourceno == 1) { cnurlxp = cnurlxp.Replace("cnface", (i * 2).ToString()); cnnamexp = cnnamexp.Replace("cnface", (i * 2).ToString()); cnbodyXp = cnbodyXp.Replace("cnface", i.ToString()); cntimeXp = cntimeXp.Replace("cnface", (i * 3).ToString()); } if (sourceno == 2) { cnurlxp = cnurlxp.Replace("cnface", i.ToString()); cnnamexp = cnnamexp.Replace("cnface", i.ToString()); cnbodyXp = cnbodyXp.Replace("cnface", i.ToString()); cntimeXp = cntimeXp.Replace("cnface", i.ToString()); urlllog = "http://www.skycn.com/"; } //取接口地址 var cnurl = urlllog + doc.DocumentNode.SelectSingleNode(cnurlxp).Attributes["href"].Value; if (cnurlRgx != null && !cnurlRgx.Equals("")) cnurl = new Regex(cnurlRgx).Match(cnurl).Groups[1].Value; //取接口名称 var title = doc.DocumentNode.SelectSingleNode(cnnamexp).InnerHtml; if (cnnameRgx != null && !cnnameRgx.Equals("")) title = new Regex(cnnameRgx).Match(title).Groups[1].Value; title = new HtmlToText().ConvertHtml(title).Trim(); //取接口内容 var body = doc.DocumentNode.SelectSingleNode(cnbodyXp).InnerHtml; if (cnbodyRgx != null && !cnbodyRgx.Equals("")) title = new Regex(cnbodyRgx).Match(body).Groups[1].Value; body = new HtmlToText().ConvertHtml(body).Trim(); //取接口发布时间 var ptStr = doc.DocumentNode.SelectSingleNode(cntimeXp).InnerHtml; if (cntimeRgx != null && !cntimeRgx.Equals("")) ptStr = new Regex(cntimeRgx).Match(ptStr).Groups[1].Value; CnInterfaceInfo cnf = new CnInterfaceInfo { CnInterfaceTypeID = cntypeid, CnInterfaceName = title, CnInterfacebody = body, CnInterfaceUrl = cnurl, CnInterfaceSource = cnsource, CnInterfaceSourceUrl = cnsourceurl, CnInterfaceAppearTime = Convert.ToDateTime(ptStr) }; InterfaceInfo IFI = new InterfaceInfo(); int cont= IFI.InsertCnInterfaceInfo(cnf); if (cont > 0) insertcount += cont; } catch { continue; } System.Threading.Thread.Sleep(200); } return insertcount; }