c#如何获取shtml页面的html源码

霍霍磨刀 2020-01-14 09:14:17
想用C#爬个网页,比如http://www.chinanews.com/world.shtml
使用网上查的方法,比如WebClient,HttpWebRequest等都不行啊,获取的是如下内容:
<!DOCTYPE html>\n<html>\n<head>\n<meta id=\"viewport\" name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0\" />\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n</head>\n<body>\n<script type=\"text/javascript\">\n//获取url参数\nfunction getUrlParam(name) {\n var reg = new RegExp(\"(^|&)\" + name + \"=([^&]*)(&|$)\", \"i\"),\n r = decodeURIComponent(window.location.search).substr(1).match(reg);\n return r!=null ? decodeURI(r[2]) : null;\n}\n\n(function(){\n\tvar ratio = window.devicePixelRatio || 1;\n\tvar UA = String(navigator.userAgent).toLowerCase();\n\t//alert(navigator.userAgent)\n\t//var hasTouch = document.documentElement.ontouchstart !== undefined;\n\tvar isMobile = UA.indexOf('mobile') > -1;\n\t//var isAndroid = UA.indexOf('android') > -1;\n\tvar isIPad = UA.indexOf('ipad') > -1;\n\t//var isIPhone = UA.indexOf('iphone') > -1;\n\t//此方法误判很严重\n\t//var isBigScreen = Math.min(screen.height/ratio,screen.width/ratio) > 600;\n\t//针对点一点扫一扫,跳转结果界面\n\tvar g_type = getUrlParam(\"type\");\n\tvar g_tips = getUrlParam(\"tips\");\n\tvar tabs = getUrlParam(\"tabs\");\n\tvar sms = getUrlParam(\"sms\");\n\t\n\t//检测平台\n\tvar p = String(navigator.platform.toLowerCase()),\n\t\tisWin = p.indexOf(\"win\") == 0, \n\t\tisMac = p.indexOf(\"mac\") == 0, \n\t\tisX11 = (p == \"x11\") || (p.indexOf(\"linux\") == 0); \n\t\t\n\tvar template = getUrlParam(\"template\") || \"default\";\n\tvar $href = '';\n\t//1、not mobile //pc\n\t//2、mobile but ipad\n\t//3、big screen //android pad\n\t//4、检测平台\n\tif((isMobile && !isIPad)||!(isWin||isMac||isX11||isIPad)){\n\t\t$href = \"mobile.html\";\n\t\tif(tabs == \"wx\"){\n\t\t\tif (sms != 1){\n\t\t\t\tif(g_type==\"logout\") {\n\t\t\t\t\tlocation.href = template+\"/result.html?type=success\";\n\t\t\t\t\treturn;\n\t\t\t\t} else if(g_type==\"authfailed\") {\n\t\t\t\t\tlocation.href = template+\"/result.html?type=failure&g_tips=\"+g_tips;\n\t\t\t\t\treturn;\n\t\t\t\t} else if(g_type==\"authalready\") {\n\t\t\t\t\tlocation.href = template+\"/result.html?type=warn&g_tips=\"+g_tips;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$href = \"mobile_sms.html\";\n\t\t\t\tlocation.href = template+\"/\"+$href+location.search;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\t\t\n\t}else{\n\t\t$href = \"pc.html\";\n\t}\t\n\tlocation.href = template+\"/\"+$href+location.search;\n})();\n</script>\n</body>\n</html>\n\n

求高手解决
...全文
307 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wooddy1982 2020-01-16
  • 打赏
  • 举报
回复
因为 WebClient, HttpWebRequest 没加入浏览器信息,爬不到源码
霍霍磨刀 2020-01-16
  • 打赏
  • 举报
回复
引用 3 楼 WQR1994 的回复:
这个不就是源码吗
这个和浏览器打开的后再“查看网页源代码”的源码不一样 我是想把里面的新闻爬下来,不过这两天查到一个httpHelper,正在研究,不知行不行
X-i-n 2020-01-16
  • 打赏
  • 举报
回复
用selenium
stherix 2020-01-15
  • 打赏
  • 举报
回复
动态网页 怎么可能获取到源码呢
data-v 2020-01-15
  • 打赏
  • 举报
回复
这个不就是源码吗
霍霍磨刀 2020-01-15
  • 打赏
  • 举报
回复
只需要获取解析后的html就行,就像用浏览器打开后,“查看网页源代码”一样

110,533

社区成员

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

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

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