抓取网站页面的内容,简单的类应用,代码如下:

 /// <summary>
/// 获取页面内容
/// </summary>
/// <param name="url">Url链接</param>
/// <returns></returns>
public string WebHtmlCon(string url)
{
try
{
string htmlCon = "";
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
using (StreamReader sReader = new StreamReader(stream, Encoding.UTF8))
{
htmlCon = sReader.ReadToEnd();
}
return htmlCon;
}
catch (Exception e)
{
return e.Message;
}
}
/// <summary>
/// 获取页面内容
/// </summary>
/// <param name="url">Url链接</param>
/// <returns></returns>
public string WebClientHtmlCon(string url)
{
try
{
WebClient webclient = new WebClient();
webclient.Encoding = Encoding.UTF8;
string HtmlCon = webclient.DownloadString(url);
return HtmlCon;
}
catch (Exception E)
{
return E.Message;
}
}

根据抓取的内容进行实际应用。

例如api的应用,获取页面内容的json数据,并进行分析获取自己想要的数据:

json数据分析代码(引用的.Net自带的类库应用):

         /// <summary>
/// JSON数据解析 返回字典类 引用:System.Web.Extensions 类库
/// </summary>
/// <param name="jsonData">json数据</param>
/// <returns></returns>
private static Dictionary<string, object> JsonToDictionary(string jsonData)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Deserialize<Dictionary<string, object>>(jsonData);
}

快递查询API应用:

  /// <summary>
/// 查询邮件的邮寄状况
/// </summary>
/// <param name="con">邮寄公司</param>
/// <param name="number">邮寄号</param>
/// <returns></returns>
public static string SelectYJ(string con, string number)
{
string url = "http://www.kuaidi100.com/query?type=" + con + "&postid=" + number;//查询地址 Dictionary<string, object> diclist = new Dictionary<string, object>();
diclist = JsonToDictionary(WebHtmlCon(url)); if (diclist["message"].ToString() == "ok")
{
string KuaiDi = "<table id=\"showtablecontext\" style=\"border-collapse: collapse; width:520px; border-spacing: 0; border:0;\">";
KuaiDi += "<tbody>";
KuaiDi += "<tr><th width='163' style=\"background: #64AADB; border: 1px solid #75C2EF; color: #FFFFFF; font-size: 14px; font-weight: bold; height: 28px; line-height: 28px; text-indent: 15px;\">时间</th><th width='354' style=\"background: #64AADB; border: 1px solid #75C2EF; color: #FFFFFF; font-size: 14px; font-weight: bold; height: 28px; line-height: 28px; text-indent: 15px;\">地点和跟踪进度</th></tr>";
ArrayList list = (ArrayList)diclist["data"]; foreach (Dictionary<string, object> item in list)
{ KuaiDi += " <tr><td style=\"border: 1px solid #DDDDDD; font-size: 12px; line-height: 22px; padding: 3px 5px;\">" + item["time"].ToString() + "</td><td>" + item["context"].ToString() + "</td></tr>"; }
KuaiDi += "</tbody>";
KuaiDi += "</table>";
return KuaiDi;
}
else
{
return "<p style=\"line-height:28px;margin:0px;padding:0px;color:#F21818; font-size: 14px;\">快递公司网络异常,请稍后查询.</p>";
}
}

结果为:

C# 页面抓取类的更多相关文章

  1. 新浪新闻页面抓取(JAVA-Jsoup)

    1.使用gradle建立工程: 工程格式如下: include ':spider-demo' rootProject.name = 'my-spider-demo' settings def void ...

  2. 分享一个c#t的网页抓取类

    using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Ne ...

  3. C# 页面抓取获取快递信息

    通过页面抓取信息可以获得很多我们想要的信息,比如现在常会用到的快递查询,主要抓取的网站为http://www.kuaidi100.com/ 通过IE的网络分析我们可以得到下面信息 通过对这个网站的分析 ...

  4. [python]初试页面抓取——抓取沪深股市交易龙虎榜数据

    [python]抓取沪深股市交易龙虎榜数据 python 3.5.0下运行 没做自动建立files文件夹,需要手动在py文件目录下建立files文件夹后运行 #coding=utf-8 import ...

  5. java 网页页面抓取标题和正文

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  6. 【Python3 爬虫】01_简单页面抓取

    运行平台:Winodows 10 Python版本:Python 3.4.2 IDE:Sublime text3 网络爬虫 网络爬虫,也叫网络蜘蛛(Web Spider),如果把互联网比喻成一个蜘蛛网 ...

  7. Java HTML页面抓取实例

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  8. php curl抓取类分享

    class UsualFunForNetWorkHelper { /*** * post请求数据 */ public static function HttpsPost($url, $data = n ...

  9. AutoIT: 通过页面抓取来陈列任务管理器里面所有进程的列表

    #include<Array.au3> $handle =WinGetHandle("Windows 任务管理器") ;$ctrl =ControlGetHandle( ...

随机推荐

  1. MFC学习 标签页与属性页及各常用控件使用

    参考 http://blog.csdn.net/anye3000/article/details/6700023 CTabCtrl: BOOL CTabTestDlg::OnInitDialog() ...

  2. Virtualenv介绍

    [翻译]http://virtualenv.readthedocs.org/en/latest/index.html virtualenv是创建独立python环境的一种工具. 环境搭建的过程中,有一 ...

  3. MWC飞控增加声纳定高的方法

    MWC飞控增加声纳定高的方法 2015.12.17 更新:经过2个周末的上机测试,该算法效果很好,在低空超声锁高之后离地高度非常稳定,现在已经成功应用在低空航拍上了. 现状 MWC开源飞控已经很有点年 ...

  4. 【LeetCode】13. Roman to Integer 罗马数字转整数

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  5. memcached学习(4). memcached的分布式算法

    memcached的分布式 正如第1次中介绍的那样, memcached虽然称为"分布式"缓存服务器,但服务器端并没有"分布式"功能. 服务器端仅包括 第2次. ...

  6. nginx 编译模块说明

    --prefix= <path> - Nginx安装路径.如果没有指定,默认为 /usr/local/nginx. --sbin-path= <path> - Nginx可执行 ...

  7. 学习opencv跟轮廓相关的

    查找轮廓 轮廓到底是什么?一个轮廓一般对应一系列的点,也就是图像中的一条曲线.表示的方法可能根据不同情况而有所不同.有多重方法可以表示曲线.在openCV中一般用序列来存储轮廓信息.序列中的每一个元素 ...

  8. 辅助的写与数据库交互的XML文件的类

    现在企业级WEB应用中与数据库交互的XML文件都是通过插件自动生成的,不过有些时候修改比较老的项目的时候也是需要手动的来做这一动作的!如下代码就是一个实现上述的功能的辅助类,在此记录一下以备后用! p ...

  9. html中button的type属性

         接触web开发不久,今天遇到了一个问题,点击button按钮,浏览器没有反应,尝试了自己可以想到的所有办法,还是无果.只得请教他人,才发现是button的type属性搞得怪,原来:     ...

  10. 动态链接库(dll) __declspec(dllimport) __declspec(dllexport)

    一. __declspec(dllexport) Microsoft 在 Visual C++ 的 16 位编译器版本中引入了 __export,使编译器得以自动生成导出名并将它们放到一个 .lib ...