Html Agility Pack在另一个网站上执行搜索并进行解析

时间:2016-11-08 17:20:33

标签: c# html-agility-pack

我正在尝试创建跟踪页面,该页面将在多个网站上进行搜索并解析结果。到目前为止,我创建了控制器和视图,但它无法正常工作。任何想法如何处理?

我的cshtml



<form id="form1" runat="server">
    <div>
        <input ID="TextBox1" runat="server">
        <button type="button" class="btn btn-default" ID="Button1"
                runat="server" Text="ara" onclick="Button1_Click"></button>

        <label ID="Label1" runat="server" Text=""></label>
    </div>
</form> 
&#13;
&#13;
&#13;

和控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;  // HttpWebRequest
using System.IO;  // Stream
using HtmlAgilityPack;  // Html parse
using System.Text;  // Encoding

namespace Nop.Web.Controllers
{
    public class ContainerTrackingController : Controller
    {
        // GET: ContainerTracking
        public ActionResult Index()
        {
            return View();
        }
    }
}

namespace arkasline
{
    class Program
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            Uri ur = null;
            Label1.Text = "";

            // Gets results from Arkasline
                // Prepare query on Arkasline
                string arkaslinesearch = "http://tracking.arkasline.com/ContainerTracking/" + "&q=";

                // Adding query to search
                ur = new Uri(arkaslinesearch + TextBox1.Text);

                // Sending request to Arkasline
                HttpWebRequest arkaslinerequest = (HttpWebRequest)WebRequest.Create(ur);
                // Getting response from Arkasline
                HttpWebResponse arkaslineget = (HttpWebResponse)arkaslinerequest.GetResponse();
                // Encoding request
                Encoding getencode = Encoding.GetEncoding(arkaslineget.CharacterSet);
                // Reading Respose
                StreamReader arkaslinestream = new StreamReader(arkaslineget.GetResponseStream(), getencode);

                // Saving response as string
                string html = arkaslinestream.ReadToEnd();

                // Parsing response
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);

                // Response on selected nodes
                HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//*[@id=\"cphMain_pnlMessage\"]/table/tbody/tr/td");

                // Adding results to Label1
                Label1.Text += "Result :<br />";
                // counting
                int i = 1;
                try
                {
                    Label1.Text += "<br /><br />";
                }
                catch (Exception exp)
                {
                    Label1.Text += "<font color=\"red\"> No Results found! </font> <br /><br />";
                }
        }

        private class Label1
        {
            public static string Text { get; internal set; }
        }

        private class TextBox1
        {
            public static string Text { get; internal set; }
        }
    }
}

0 个答案:

没有答案