打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
网页数据抓取demo

//后台端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //要抓取的URL地址
            string Url = "http://www.gdwb.gov.cn/Service/GetRiverOneList.aspx";
            //得到指定Url的源码
            string strWebContent = GetWebContent(Url);

            //防止网络速度慢,获取不到网站源码,循环直到获取为止,不然下面会报错
            while (strWebContent == null && strWebContent == "")
            {
                strWebContent = GetWebContent(Url);
            }
            try
            {
                //取出和数据有关的那段源码
                //int iBodyStart = strWebContent.IndexOf("<body>", 0);
                //int iStart = strWebContent.IndexOf("<form", iBodyStart);
                int iTableStart = strWebContent.IndexOf("<table cellspacing=\"0\" cellpadding=\"5\" border=\"0\" width=\"100%\">", 0);
                int iTableEnd = strWebContent.IndexOf("</form>");
                string strWeb = strWebContent.Substring(iTableStart, iTableEnd - iTableStart);
                //获取数据存入数据库

                //

                WebBrowser webb = new WebBrowser();
                webb.Navigate("about:blank");
                HtmlDocument htmldoc = webb.Document.OpenNew(true);
                htmldoc.Write(strWeb);
                HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("table");


                //航道名称
                string NgChannelName = "";
                //航道水位站
                string NgChannelGage = "";
                //上报日期06-05
                string NgChannelDate = "";
                //水位
                string NgWaterLevel = "";
                //前一天
                string NgPreData = "";
                //较前一天
                string NgPreBefore = "";
                //抓取时间
                string NgGrabTime = DateTime.Now.ToString();
                //来源
                string NgSource = "http://www.gdwb.gov.cn/Service/GetRiverOneList.aspx";
                //版本号
                string NgVersionNum = string.Format("{0}{1}{2}{3}{4}{5}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);



                int code = 0;   //本页面有2个table的话,解析成三个table数组,可设置code防止index越界
                foreach (HtmlElement tr in htmlTR)
                {
                    code++;
                    if (code == 2)
                    {
                        continue;
                    }
                    else
                    {
                        if (code < 4)
                        {
                            int i;
                            if (code == 1)
                            {
                                for (i = 0; i < 12; i++)
                                {
                                    StringBuilder sql = new StringBuilder();
                                    //第一行是标题栏,因此特殊处理
                                    if (i == 0)
                                    { continue; }
                                    else
                                    {
                                        //航道名称
                                        NgChannelName = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[0].InnerText;
                                        //航道水位站
                                        NgChannelGage = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[1].InnerText;
                                        //上报日期06-05
                                        NgChannelDate = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[2].InnerText;
                                        //水位
                                        NgWaterLevel = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[3].InnerText;
                                        //前一天
                                        NgPreData = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[4].InnerText;
                                        //较前一天
                                        NgPreBefore = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[5].InnerText;


                                        //if (NgChannelName.Contains("西江"))
                                        //{
                                        //    sql.Append("insert into ng_channel_water(ng_channel_name,ng_channel_gage,ng_channel_date,ng_water_level,ng_pre_data,");
                                        //    sql.Append("ng_pre_before,ng_grab_time,ng_source,ng_version_num,operate_person,");
                                        //    sql.Append("operate_time,operate_type)");
                                        //    sql.AppendFormat(" VALUES('{0}','{1}','{2}','{3}','{4}',", NgChannelName, NgChannelGage, NgChannelDate, NgWaterLevel, NgPreData);
                                        //    sql.AppendFormat(" '{0}','{1}','{2}','{3}','{4}',", NgPreBefore, NgGrabTime, NgSource, NgVersionNum, OperatePerson);
                                        //    sql.AppendFormat(" '{0}','{1}')", OperateTime, OperateType);


                                        //    string selSql = string.Format("SELECT * from ng_channel_water where ng_channel_gage='{0}' AND ng_water_level='{1}' AND ng_channel_date='{2}'", NgChannelGage, NgWaterLevel, NgChannelDate);
                                        //    //判断是否存在
                                        //    DataSet ds = CommonDao.GetDaoMySql().GetDataSetBySql(selSql);
                                        //    if (ds.Tables[0].Rows.Count < 1)
                                        //    {
                                        //        CommonDao.GetDaoMySql().Add(sql.ToString());
                                        //    }
                                        //    else
                                        //    {
                                        //        continue;
                                        //    }
                                        //}
                                    }
                                }
                            }
                            else
                            {
                                for (i = 0; i < 11; i++)
                                {
                                    StringBuilder sql = new StringBuilder();

                                    //航道名称
                                    NgChannelName = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[0].InnerText;
                                    //航道水位站
                                    NgChannelGage = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[1].InnerText;
                                    //上报日期06-05
                                    NgChannelDate = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[2].InnerText;
                                    //水位
                                    NgWaterLevel = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[3].InnerText;
                                    //前一天
                                    NgPreData = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[4].InnerText;
                                    //较前一天
                                    NgPreBefore = tr.GetElementsByTagName("tr")[i + 1].GetElementsByTagName("td")[5].InnerText;


                                    //if (NgChannelName.Contains("西江"))
                                    //{
                                    //    sql.Append("insert into ng_channel_water(ng_channel_name,ng_channel_gage,ng_channel_date,ng_water_level,ng_pre_data,");
                                    //    sql.Append("ng_pre_before,ng_grab_time,ng_source,ng_version_num,operate_person,");
                                    //    sql.Append("operate_time,operate_type)");
                                    //    sql.AppendFormat(" VALUES('{0}','{1}','{2}','{3}','{4}',", NgChannelName, NgChannelGage, NgChannelDate, NgWaterLevel, NgPreData);
                                    //    sql.AppendFormat(" '{0}','{1}','{2}','{3}','{4}',", NgPreBefore, NgGrabTime, NgSource, NgVersionNum, OperatePerson);
                                    //    sql.AppendFormat(" '{0}','{1}')", OperateTime, OperateType);


                                    //    string selSql = string.Format("SELECT * from ng_channel_water where ng_channel_gage='{0}' AND ng_water_level='{1}' AND ng_channel_date='{2}'", NgChannelGage, NgWaterLevel, NgChannelDate);
                                    //    //判断是否存在
                                    //    DataSet ds = CommonDao.GetDaoMySql().GetDataSetBySql(selSql);
                                    //    if (ds.Tables[0].Rows.Count < 1)
                                    //    {
                                    //        CommonDao.GetDaoMySql().Add(sql.ToString());
                                    //    }
                                    //    else
                                    //    {
                                    //        continue;
                                    //    }
                                    //}
                                }
                            }

                            Label1.Text = NgChannelName;
                            Label2.Text = NgChannelGage;
                             Label3.Text = NgChannelDate;
                           Label4.Text = NgWaterLevel;
                           Label5.Text = NgPreData;
                           Label6.Text = NgPreBefore;

                        }
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            }

        /// <summary>
        /// 获取该URL的HTML内容
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        private string GetWebContent(string Url)
        {
            string strResult = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                //声明一个HttpWebRequest请求
                request.Timeout = 30000;
                //设置连接超时时间
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.GetEncoding("UTF-8");
                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                strResult = streamReader.ReadToEnd();
            }
            catch
            {
                MessageBox.Show("出错");
            }
            return strResult;
        }

        
    }
}



//前台端
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default"aspcompat=true %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>     
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                    <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
                        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>

    </div>
    </form>
</body>
</html>


!出现单线程问题在<%page      >内加     aspcompat=true     ,例<%page    aspcompat=true  >   

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c# 抓取Web网页数据分析
在.net界面中修改web.config/app.config配置
用Excel来写个爬虫怎么样?
C#基础知识
.net开发微信公众平台
Web Service 三种身份验证方式-程序开发-红黑联盟
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服