asp.net C# 获取网页源代码的几种方式
2024-10-20 09:24:19
1 方法
System.Net.WebClient aWebClient = new System.Net.WebClient();
aWebClient.Encoding = System.Text.Encoding.Default;
Byte[] pageData = aWebClient.DownloadData(url); string nhtml = Encoding.GetEncoding("utf-8").GetString(pageData);
2方法
System.Net.WebClient aWebClient = new System.Net.WebClient();
aWebClient.Encoding = System.Text.Encoding.Default;
string nhtml = aWebClient.DownloadString(goodstidurl);
3方法
WebBrowser webbrowser = new WebBrowser();
StreamReader sr = new StreamReader(this.webBTaobao.DocumentStream, Encoding.Default);
html = sr.ReadToEnd();
html = html.Replace("\r\n", "");
html = html.Replace("\n", "");
html = html.Replace(" ", "");
html = html.Replace("(", "");
html = html.Replace(")", "");
string nurl = Regex.Match(html, "(?<=data-url=\").*?(?=\")").Value;
//新建一个WebBrowser
WebBrowser webAddress = new WebBrowser();
webAddress.Navigate(nurl);
//等待载入完毕
while (webAddress.ReadyState < WebBrowserReadyState.Complete) Application.DoEvents();
StreamReader sraddress = new StreamReader(webAddress.DocumentStream, Encoding.Default);
jsonaddress = sraddress.ReadToEnd();
4方法
WebRequest hwr = WebRequest.Create(@"http://item.taobao.com/item.htm? id=" + row["urlId"].ToString());//向指定Url发出请求
HttpWebResponse hwp = hwr.GetResponse() as HttpWebResponse;//将hwr对HTTP的请求
string text;
StreamReader sr;
string code = hwp.ContentType;//请求响应得到的内容类型
//得到编码了
code = code.Split('=')[1];
Stream rep = hwp.GetResponseStream();//将请求得到的内容以流的形式读出
sr = new StreamReader(rep, Encoding.GetEncoding(code));//用指定的字符编码为指定的流初始化
最新文章
- 修改C:\WINDOWS\system32\drivers\etc\hosts 文件有什么作用
- 软件打开时间、窗体透明度、背景色---《用delphi开发共享软件》-15.1任务管理器
- Lua协程
- LINQ SQL分组取最近一条记录
- js封装,一个JS文件引用多个JS文件
- 对于python的内存管理的好文章
- Drools引擎学习
- centos6.4 ceph安装部署之ceph object storage
- UVA 11080 - Place the Guards(二分图判定)
- android台 cocos2d-x 翻阅相册数据
- WebApi2 文件图片上传下载
- Windows 定时任务对数据库进行操作
- 【学习总结】GirlsInAI ML-diary day-15-读/写txt文件
- 《深入理解java虚拟机》读书笔记——垃圾收集与内存分配策略
- Layui 写一个简单的后台页面
- 四种对话框(dialog)的简单使用方法
- 题解 P2580 【于是他错误的点名开始了】
- eclipse JRE(unbound)问题
- ubuntu 16.04 appstreamcli 问题
- Code Quality
热门文章
- [BOI2011]MET-Meteors
- BZOJ 1415 [NOI2005]聪聪与可可 (概率DP+dfs)
- WIN10 java环境变量问题之 配置的JDK1.8版本却是1.7
- SpringBoot中打包设置,将配置文件打包在外部
- [Tools] Using mobile device for debugging your mobile web site
- [Angular] Set Metadata in HTTP Headers with Angular HttpHeaders
- cocos2d-x 移植到android中编译的一些问题:fatal error: Box2D/Box2D.h: No such file or directory&;quot;
- Hive编程指南_学习笔记01
- 为QML创建C++插件(下载)
- (八)统一配置中心-Config