c#要获取网页的内容,而非源代码,将获取的内容写进我的资料库中,请大侠帮忙处理

解决方案 »

  1.   


    WebBrowser web = new WebBrowser();
                web.Navigate("http://www.xjflcp.com/ssc/");
                web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);
    void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                WebBrowser web = (WebBrowser)sender;
                HtmlElementCollection ElementCollection = web.Document.GetElementsByTagName("Table");
                foreach (HtmlElement item in ElementCollection)
                {
                     File.AppendAllText("Kaijiang_xj.txt", item.InnerText);
                }
            }
      

  2.   


    //"开始"按钮事件     
      private   void   button1_Click(object   sender,   System.EventArgs   e)   {     
      //取得预保存的文件名     
      string   fileName=textBox3.Text.Trim();     
      //远程主机     
      string   hostName=textBox1.Text.Trim();     
      //端口     
      int   port=Int32.Parse(textBox2.Text.Trim());     
      //得到主机信息     
      IPHostEntry   ipInfo=Dns.GetHostByName(hostName);     
      //取得IPAddress[]     
      IPAddress[]   ipAddr=ipInfo.AddressList;     
      //得到ip     
      IPAddress   ip=ipAddr[0];     
      //组合出远程终结点     
      IPEndPoint   hostEP=new   IPEndPoint(ip,port);     
      //创建Socket   实例     
      Socket   socket=new   Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);     
      try     
      {     
      //尝试连接     
      socket.Connect(hostEP);     
      }     
      catch(Exception   se)     
      {     
      MessageBox.Show("连接错误"+se.Message,"提示信息     
      ,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);     
      }     
      //发送给远程主机的请求内容串     
      string   sendStr="GET   /   HTTP/1.1\r\nHost:   "   +   hostName   +     
      "\r\nConnection:   Close\r\n\r\n";     
      //创建bytes字节数组以转换发送串     
      byte[]   bytesSendStr=new   byte[1024];     
      //将发送内容字符串转换成字节byte数组     
      bytesSendStr=Encoding.ASCII.GetBytes(sendStr);     
      try     
      {     
      //向主机发送请求     
      socket.Send(bytesSendStr,bytesSendStr.Length,0);     
      }     
      catch(Exception   ce)     
      {     
      MessageBox.Show("发送错误:"+ce.Message,"提示信息     
      ,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);     
      }     
      //声明接收返回内容的字符串     
      string   recvStr="";     
      //声明字节数组,一次接收数据的长度为1024字节     
      byte[]   recvBytes=new   byte[1024];     
      //返回实际接收内容的字节数     
      int   bytes=0;     
      //循环读取,直到接收完所有数据     
      while(true)     
      {     
      bytes=socket.Receive(recvBytes,recvBytes.Length,0);     
      //读取完成后退出循环     
      if(bytes〈=0)     
      break;     
      //将读取的字节数转换为字符串     
      recvStr+=Encoding.ASCII.GetString(recvBytes,0,bytes);     
      }     
      //将所读取的字符串转换为字节数组     
      byte[]   content=Encoding.ASCII.GetBytes(recvStr);     
      try     
      {     
      //创建文件流对象实例     
      FileStream   fs=new   FileStream(fileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);     
      //写入文件     
      fs.Write(content,0,content.Length);     
      }     
      catch(Exception   fe)     
      {     
      MessageBox.Show("文件创建/写入错误:"+fe.Message,"提示信息",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);     
      }     
      //禁用Socket     
      socket.Shutdown(SocketShutdown.Both);     
      //关闭Socket     
      socket.Close();     
      }     
      }     
      

  3.   

    谢谢两位的帮忙,我要的c# Form 程序获取网页的页面内容,请再顶!!! 
      

  4.   

    看你问题,似乎是你站点怎么拿到它的源码,那我就告诉你,通过WebRequest拿回来的都是页面输出的完整的HTML源码,你必须根据源码的格式,用正则来过滤出你需要的内容部分。
      

  5.   

    WebRequest可以,就几句话就可以拿到另外算法分什么Form和非Form,有了方法你在哪都能用,为什么非要Form呢,这也太不专业了吧微软把人都教傻了
      

  6.   

    WebRequest可以实现请写出详细的代码!!我需要详细的代码而不是说一个方法
      

  7.   

    private void BtnGetPage_Click(object sender, EventArgs e)
            {
                //指定请求
                HttpWebRequest request =(HttpWebRequest) WebRequest.Create("http://www.baidu.com");            //得到返回
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();            //得到流
                Stream recStream = response.GetResponseStream();            //编码方式
                Encoding gb2312 = Encoding.GetEncoding("gb2312");            //指定转换为gb2312编码
                StreamReader sr = new StreamReader(recStream, gb2312);            //以字符串方式得到网页内容
                String content = sr.ReadToEnd();            //将网页内容显示在TextBox中
                textBox1.Text = content;
            }需要
    using System.Net;
    using System.IO;结果就是百度首页的内容:
    <html><head><meta http-equiv=Content-Type content="text/html;charset=gb2312"><title>百度一下,你就知道      </title><style>body{margin:4px 0}p{margin:0;padding:0}img{border:0}td,p,#u{font-size:12px}#b,#u,#l td,a{font-family:arial}#kw{font:16px Verdana;height:1.78em;padding-top:2px}#b{height:30px;padding-top:4px}#b,#b a{color:#77c}#u{padding-right:10px;line-height:19px;text-align:right;margin:0 0 3px !important;margin:0 0 10px}#sb{height:2em;width:5.6em}#km{height:50px}#l{margin:0 0 5px 15px}#l td{padding-left:107px}p,table{width:650px;border:0}#l td,#sb,#km{font-size:14px}#l a,#l b{margin-right:1.14em}a{color:#00c}a:active{color:#f60}#hp{position:absolute;margin-left:6px}#lg{margin:-26px 0 -44px}#lk{width:auto;line-height:18px;vertical-align:top}form{position:relative;z-index:9}</style></head>
    <body><div id=u><a href=http://passport.baidu.com/?login&tpl=mn>登录</a></div><center><img src=http://www.baidu.com/img/baidu_logo_jr_0912_shd.gif width=270 height=129 usemap="#mp" id=lg><br><br><br><br><table cellpadding=0 cellspacing=0 id=l><tr><td><div id=m><a onclick=s(this) href=http://news.baidu.com>新&nbsp;闻</a><b>网&nbsp;页</b><a onclick=s(this) href=http://tieba.baidu.com>贴&nbsp;吧</a><a onclick=s(this) href=http://zhidao.baidu.com>知&nbsp;道</a><a onclick=s(this) href=http://mp3.baidu.com>MP3</a><a onclick=s(this) href=http://image.baidu.com>图&nbsp;片</a><a onclick=s(this) href=http://video.baidu.com>视&nbsp;频</a></div></td></tr></table>
    <table cellpadding=0 cellspacing=0 style="margin-left:15px"><tr valign=top><td style="height:62px;padding-left:92px" nowrap><form name=f action=s><input type=text name=wd id=kw size=42 maxlength=100> <input type=submit value=百度一下 id=sb><span id=hp><a href=/gaoji/preferences.html>设置</a><br><a href=/gaoji/advanced.html>高级</a></span></form></td></tr></table>
    <p id=km><a href=http://hi.baidu.com>空间</a>&nbsp;&nbsp;<a href=http://www.hao123.com>hao123</a>&nbsp;|&nbsp;<a href=/more/>更多<span style="font-family:宋体">>></span></a></p>
    <p style=height:60px><table cellpadding=0 cellspacing=0 id=lk><tr><td></td></tr></table></p>
    <p style=height:30px><a onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href=http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com>把百度设为主页</a></p><p style=height:14px><a href=http://e.baidu.com/?refer=888>加入百度推广</a> | <a href=http://top.baidu.com>搜索风云榜</a> | <a href=http://home.baidu.com>关于百度</a> | <a href=http://ir.baidu.com>About Baidu</a></p><p id=b>&copy;2009 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://www.miibeian.gov.cn target=_blank>京ICP证030173号</a> <img src=http://gimg.baidu.com/img/gs.gif></p><map name=mp><area shape=rect coords="43,22,227,91" href=http://christmas.baidu.com/ target=_blank title="点此进入 圣诞专题页面"></map></center></body>
    <script>var w=document.f.wd;function s(o){if(w.value.length>0){var h=o.href;var q=encodeURIComponent(w.value);if(h.indexOf("q=")!=-1){o.href=h.replace(new RegExp("q=[^&$]*"),"q="+q)}else{o.href+="?q="+q}}};(function(){if(new RegExp("q=([^&]+)").test(location.search)){w.value=decodeURIComponent(RegExp.$1)}})();if(navigator.cookieEnabled&&!/sug?=0/.test(document.cookie)){document.write('<script src=http://www.baidu.com/js/bdsug.js?v=1.1.0.3><\/script>')};if(window.attachEvent){window.attachEvent("onload",function(){w.focus();})}else{window.addEventListener('load',function(){w.focus()},true)};window.onunload=function(){};</script></html><!--5d8982f97d8fc021-->
      

  8.   

    楼上没有理解我的意思,我要的结果是要去掉那些HTML代码所剩下的内容     如 HttpWebRequest request =(HttpWebRequest) WebRequest.Create("http://www.baidu.com");
         得到:新 闻 网 页 贴 吧 知 道 MP3图 片 视 频    这样的结果
              
      

  9.   

    无论你想要什么,这一步是你必须的!  你想要的内容只能你自己根据实际情况用正则表达式来解析,如果你想用通用的方法来解析这些内容,C#下我只能说我目前还不知道。Java下有HTMLEditorKit这个网页解析器可以用,想用的话也不是很简单
      

  10.   

    html+dom+远程js 可以解决 如gg地图
      

  11.   

    楼主,只要一句话, 但要加一个webbrowser控件
    messagebox.show( webbrowser.Document.Body.InnerText.ToString ());
    给我分吧
      

  12.   

    http://www.cnblogs.com/sufei/archive/2011/10/22/2221289.html