当前位置: 代码迷 >> ASP.NET >> 过滤查询解决思路
  详细解决方案

过滤查询解决思路

热度:7777   发布时间:2013-02-25 00:00:00.0
过滤查询
我现在正在做一个网站 我里面包含一个查询图书的系统 但是现在我没有图书馆的数据库 我想借用别人的网页 在查询的时候让服务器在别的网页上查询 然后通过智能表达式 提取我做药查的内容 在返给客户端 想请教一下高手 这方面的知识我上那里去学 请高手给支个招 看看 怎么可以实现我的要求 谢了

------解决方案--------------------------------------------------------
我想你应该去学两点
1.正则表达式(很复杂)
2.WebRequest(很简单)

不过感觉你似乎还有更多基础的东西也要去学。
------解决方案--------------------------------------------------------
抓取页面
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;using System.IO.Compression;using System.Text.RegularExpressions;namespace WikiPageCreater.Common{    public class PageHelper    {        /// <summary>        /// 根据 url 获取网页编码        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public static string GetEncoding(string url)        {            HttpWebRequest request = null;            HttpWebResponse response = null;            StreamReader reader = null;            try            {                request = (HttpWebRequest)WebRequest.Create(url);                request.Timeout = 20000;                request.AllowAutoRedirect = false;                response = (HttpWebResponse)request.GetResponse();                if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)                {                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))                        reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));                    else                        reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII);                    string html = reader.ReadToEnd();                    Regex reg_charset = new Regex(@"charset\b\s*=\s*(?<charset>[^""]*)");                    if (reg_charset.IsMatch(html))                    {                        return reg_charset.Match(html).Groups["charset"].Value;                    }                    else if (response.CharacterSet != string.Empty)                    {                        return response.CharacterSet;                    }                    else                        return Encoding.Default.BodyName;                }            }            catch            {            }            finally            {                if (response != null)                {                    response.Close();                    response = null;                }                if (reader != null)                    reader.Close();                if (request != null)                    request = null;            }            return Encoding.Default.BodyName;        }        /// <summary>        /// 根据 url 和 encoding 获取当前url页面的 html 源代码               /// </summary>        /// <param name="url"></param>        /// <param name="encoding"></param>        /// <returns></returns>        public static string GetHtml(string url, Encoding encoding)        {            HttpWebRequest request = null;            HttpWebResponse response = null;            StreamReader reader = null;            try            {                request = (HttpWebRequest)WebRequest.Create(url);                request.Timeout = 20000;                request.AllowAutoRedirect = false;                response = (HttpWebResponse)request.GetResponse();                if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)                {                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))                        reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), encoding);                    else                        reader = new StreamReader(response.GetResponseStream(), encoding);                    string html = reader.ReadToEnd();                    return html;                }            }            catch            {            }            finally            {                if (response != null)                {                    response.Close();                    response = null;                }                if (reader != null)                    reader.Close();                if (request != null)                    request = null;            }            return string.Empty;        }    }}
  相关解决方案