zoukankan      html  css  js  c++  java
  • 抓取网页数据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";
    }

    }

    }
    }

  • 相关阅读:
    Linux入门之系统启动
    2017.12.25 Android数据存储方案
    2017.12.18 Android开发之进程讲解
    2017.12.18 Android开发之消息队列(实现子线程修改UI组件)
    2017.12.4 Android开发之ListView组件
    2017.12.10 把KIE的jbpm_console嵌入到我们的web项目中
    2017.12.07 React组件嵌套以及for循环渲染子组件
    2017.12.07 React路由到不同组件界面
    2017.12.07 Ajax获取服务器数据并发送到前端
    B-树&B+树以及其在数据库中的应用
  • 原文地址:https://www.cnblogs.com/to-creat/p/5248350.html
Copyright © 2011-2022 走看看