抓取网页数据C#文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

using System.Net;

using System.IO;

namespace WebJSON.Controllers
{
public class LibraryController : Controller
{
//
// GET: /Library/

public String Index( )
{
string pageHtml;
String value="";
List<String> a = new List<String>();

try {

WebClient MyWebClient = new WebClient();


MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据

Byte[] pageData = MyWebClient.DownloadData("http://lib.ecust.edu.cn:8081/GATESEAT/LRP.ASPX"); //从指定网站下载数据

// string pageHtml = Encoding.Default.GetString(pageData); //如果获取网站页面采用的是GB2312,则使用这句

pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句


Regex reg = new Regex("\">(.*)</f");


MatchCollection mc = reg.Matches(pageHtml);

foreach (Match match in mc)
{
a.Add(match.Value);
value = value + match.Value;
}
String floor1 = a[2].Remove(0, 36);//1楼人数
floor1 = floor1.Remove(floor1.Length-3,3);
String floor12 = a[3].Remove(0, 36);//1楼剩余座位
floor12 = floor12.Remove(floor12.Length-3,3);
String floor21 = a[5].Remove(0, 36);//2楼人数
floor21 = floor21.Remove(floor21.Length - 3, 3);
String floor22 = a[6].Remove(0, 36);//2楼剩余座位
floor22 = floor22.Remove(floor22.Length - 3, 3);
String floor31 = a[8].Remove(0, 36);//3楼人数
floor31 = floor31.Remove(floor31.Length - 3, 3);
String floor32 = a[9].Remove(0, 36);//3楼剩余座位
floor32 = floor32.Remove(floor32.Length - 3, 3);
String floor41 = a[11].Remove(0, 36);//4楼人数
floor41 = floor41.Remove(floor41.Length - 3, 3);
String floor42 = a[12].Remove(0, 36);//4楼剩余座位
floor42 = floor42.Remove(floor42.Length - 3, 3);
String floor51 = a[14].Remove(0, 36);//5楼人数
floor51 = floor51.Remove(floor51.Length - 3, 3);
String floor52 = a[15].Remove(0, 36);//5楼剩余座位
floor52 = floor52.Remove(floor52.Length - 3, 3);
String floor61 = a[17].Remove(0, 36);//6楼人数
floor61 = floor61.Remove(floor61.Length - 3, 3);
String floor62 = a[18].Remove(0, 36);//6楼剩余座位
floor62 = floor62.Remove(floor62.Length - 3, 3);


return floor41 + floor42 + floor51 + floor52 + floor61 + floor62;
}

catch(WebException webEx) {

return "0";
}

}

}
}

posted @ 2016-03-06 19:56  Wei_java  阅读(272)  评论(0编辑  收藏  举报