c# 如何获取网页的内容而非此网页的源代码 (特急)

dandan12 2009-12-25 02:04:27
c#要获取网页的内容,而非源代码,将获取的内容写进我的资料库中,请大侠帮忙处理
...全文
3643 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
苏飞论坛 2011-10-24
  • 打赏
  • 举报
回复
http://www.cnblogs.com/sufei/archive/2011/10/22/2221289.html
wubirong 2011-08-06
  • 打赏
  • 举报
回复
我晕,帖子都两年了
wubirong 2011-08-06
  • 打赏
  • 举报
回复
楼主,只要一句话, 但要加一个webbrowser控件
messagebox.show( webbrowser.Document.Body.InnerText.ToString ());
给我分吧
moshyo 2011-02-04
  • 打赏
  • 举报
回复
html+dom+远程js 可以解决 如gg地图
swb1203 2010-09-07
  • 打赏
  • 举报
回复
也请各位大侠回答一下上面的问题
swb1203 2010-09-07
  • 打赏
  • 举报
回复
我现在有个类似的,不过有区别,区别是
我想要的内容是通过调用js加载出来的内容,在网页源码中看不到那些内容,我现在得到该网页的源码后仍是得不到我想要的内容
  • 打赏
  • 举报
回复
先获取网页源码,然后在用正则匹配,你所要的内容。
icemangd 2009-12-25
  • 打赏
  • 举报
回复
顶!!!!我也有这个需求
xjdreamer 2009-12-25
  • 打赏
  • 举报
回复
无论你想要什么,这一步是你必须的! 你想要的内容只能你自己根据实际情况用正则表达式来解析,如果你想用通用的方法来解析这些内容,C#下我只能说我目前还不知道。Java下有HTMLEditorKit这个网页解析器可以用,想用的话也不是很简单
dandan12 2009-12-25
  • 打赏
  • 举报
回复
楼上没有理解我的意思,我要的结果是要去掉那些HTML代码所剩下的内容

如 HttpWebRequest request =(HttpWebRequest) WebRequest.Create("http://www.baidu.com");


得到:新 闻 网 页 贴 吧 知 道 MP3图 片 视 频 这样的结果
xjdreamer 2009-12-25
  • 打赏
  • 举报
回复


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>新 闻</a><b>网 页</b><a onclick=s(this) href=http://tieba.baidu.com>贴 吧</a><a onclick=s(this) href=http://zhidao.baidu.com>知 道</a><a onclick=s(this) href=http://mp3.baidu.com>MP3</a><a onclick=s(this) href=http://image.baidu.com>图 片</a><a onclick=s(this) href=http://video.baidu.com>视 频</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>  <a href=http://www.hao123.com>hao123</a> | <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>©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-->
马老虎 2009-12-25
  • 打赏
  • 举报
回复
我想这个是跟你的网络有关系!
有些公司或政府的网 不允许这样的请求
Tiantiandiandian 2009-12-25
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 dandan12 的回复:]
WebRequest可以实现请写出详细的代码!!我需要详细的代码而不是说一个方法
[/Quote]


不要着急,与其这样等着,都能在网上找到资料了。唉
dandan12 2009-12-25
  • 打赏
  • 举报
回复
WebRequest可以实现请写出详细的代码!!我需要详细的代码而不是说一个方法
Tiantiandiandian 2009-12-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 xjdreamer 的回复:]
WebRequest可以,就几句话就可以拿到

另外算法分什么Form和非Form,有了方法你在哪都能用,为什么非要Form呢,这也太不专业了吧

微软把人都教傻了
[/Quote]

初学者,能理解。我也是初学者
xjdreamer 2009-12-25
  • 打赏
  • 举报
回复
WebRequest可以,就几句话就可以拿到

另外算法分什么Form和非Form,有了方法你在哪都能用,为什么非要Form呢,这也太不专业了吧

微软把人都教傻了
chowyi 2009-12-25
  • 打赏
  • 举报
回复
WebBrowser
dandan12 2009-12-25
  • 打赏
  • 举报
回复
各位大侠,我也知道用正则表达式可以 实现,可是具体怎么做不知道,请给出详细的源代码。非常感谢
路人乙e 2009-12-25
  • 打赏
  • 举报
回复
内容从何得来?当然是源码了,拿到了源码也就拿到了内容(正则匹配出想要的)
Tiantiandiandian 2009-12-25
  • 打赏
  • 举报
回复
网页的源代码处理起来,通过正则表达式是没有问题滴,关键是,源代码里没有lz想要的内容。
加载更多回复(7)

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧