首页 新闻 会员 周边

CS网页登陆

0
悬赏园豆:30 [已解决问题] 解决于 2009-04-16 15:09

我现在要通过CS程序登录一个网页,然后抓取里面的内容。请教下如何才能通过CS程序登录网页???(本人对BS程序不太了解,帮忙解释的详细点啊!)

陈进原的主页 陈进原 | 初学一级 | 园豆:10
提问于:2009-04-15 16:44
< >
分享
最佳答案
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-15 17:44
其他回答(4)
0

创建一个HttpWebRequest对象,向登陆程序post用户名和密码,获取登陆的cookie,然后再用HttpWebRequest请求要抓取的页面.

Tony Lu | 园豆:32 (初学一级) | 2009-04-15 16:59
0

模拟登陆验证就可以了

persialee | 园豆:3217 (老鸟四级) | 2009-04-15 21:55
0

来晚了。

极地雪狼 | 园豆:234 (菜鸟二级) | 2009-04-15 22:59
0

方法太多了,可以用cookies,Session,application都可以实现

水默夕沙 | 园豆:215 (菜鸟二级) | 2009-04-16 09:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册