首页 新闻 会员 周边 捐助

怎么从网站上抓取数据!!

0
悬赏园豆:30 [已关闭问题]

从别的网站上抓取数据 并存到自己的数据库里怎么做到
protected void btnselect_Click(object sender, EventArgs e)
{
GetStringByUrl("http://api.showji.com/locating/Query.aspx?m="+txtTEL.Text+"");

}
public string GetStringByUrl(string strUrl)
{
WebRequest wrt = WebRequest.Create(strUrl);
WebResponse wrse = wrt.GetResponse();
Stream strM = wrse.GetResponseStream();
StreamReader SR = new StreamReader(strM, Encoding.GetEncoding("utf-8"));
string strallstrm = SR.ReadToEnd();
lblCard0.Text = strallstrm;
TextBox1.Text = strallstrm;
return strallstrm;
}

上面是我写的代码asp.net的b/s结构

从http://api.showji.com/locating/Query.aspx?m=网站上抓举 所属省份和所属城市和类型,希望各位net高手帮忙

我的qq359676564


凡哥的主页 凡哥 | 初学一级 | 园豆:170
提问于:2009-04-17 08:29
< >
分享
其他回答(1)
0

(该问题以前有人提出过、我就再回答一次吧。)用到正则表达式、HttpWebRequest的一些网络知识。看一下下边的例子、很简单啊。

   Regex regex;  
        string[] weather = new string[5];
        string content = "";
       
        Match mcTmp;
        Match mcCity;
        int k = 1;

        HttpWebResponse theResponse;
        WebRequest theRequest;


        theRequest = WebRequest.Create("http://weather.news.qq.com/inc/ss82.htm");
        try
        {
            theResponse = (HttpWebResponse)theRequest.GetResponse();

            using (System.IO.Stream sm = theResponse.GetResponseStream())
            {
                System.IO.StreamReader read = new System.IO.StreamReader(sm, Encoding.Default);
                content = read.ReadToEnd();
            }
        }
        catch (Exception)
        {
            content = "";
        }
    

        string parttenTmp = "<td height=\"23\" width=\"117\" background=\"/images/r_tembg5.gif\" align=\"center\">(?<item1>[^<]+)</td>";
        k = 1;
        regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
        {

            weather[0] = mcTmp.Groups["item1"].Value;
        }
        parttenTmp = "height=\"23\" align=\"center\">(?<item1>[^/]+)</td>";
        k = 1;
        regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
        {
            weather[k] = mcTmp.Groups["item1"].Value;
        }
        return weather;

 

这个是以前写的一个抓取天气预报的方法.你可以参考一下。

邢少 | 园豆:10926 (专家六级) | 2009-04-17 08:59
很棒
支持(0) 反对(0) 凡哥 | 园豆:170 (初学一级) | 2010-02-06 15:48
0

找一个开源的爬虫工具研究一下

winzheng | 园豆:8797 (大侠五级) | 2009-04-19 17:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册