当前位置:首页 > C#教程 > C#高级

抓取网页数据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";
}

}

}
}


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐