Perform search on other websites and parse results with HtmlAgilityPack

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 anni tempo fa
Hi guys,
I am creating a page which will perform search on shipping companies websites and will parse the results on my page.

So far I created the following controller and view but it is not working. Any ideas how to approach and achieve this?

my html
<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>


my controller
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; }
        }
    }
}
7 anni tempo fa
Solved
7 anni tempo fa
dianoche wrote:
Solved


This may help others,
Install html agility pack to your nop.Web project.

then your controller will look like this. Just arrange based on your needs,
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();
        }
        [HttpPost]
        public string GetData(string containerNo, string oper)
        {
            string result = "";
            if (ModelState.IsValid)
            {
                Uri ur = null;
                //Label1.Text = "";

                // Gets results from Arkasline
                string arkaslinesearch = "";
                // Prepare query on Arkasline
                if (oper == "Arkas")
                    arkaslinesearch = "http://tracking.test.com/ContainerTracking/?Search=" + containerNo + "&Column=ContainerNo&Page=1&token=141182191861201142356867267627129174554";
                if (oper == "MSC")
                    arkaslinesearch = "https://www.test.com/track-a-shipment?agencyPath=usa/?Search=" + containerNo;

                // Adding query to search
                ur = new Uri(arkaslinesearch );

                // 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
                try
                {
                    if (oper == "Arkas")
                {
                    HtmlNodeCollection Ship = doc.DocumentNode.SelectNodes("//*[@id=\"cphMain_gvContainer_lblVessel_0\"]");
                    HtmlNodeCollection Booking = doc.DocumentNode.SelectNodes("//*[@id=\"cphMain_gvContainer_pnlUst_0\"]/table/tr[1]/td[3]");
                // Adding results to table
                result = "<table><tr><td>Gemi Adı </td><td>Booking No</td></tr><tr><td>";
                result += Ship.FirstOrDefault().InnerText + "</td><td>"+Booking.FirstOrDefault().InnerText+"</td><tr>";
                }
                    if (oper == "MSC")
                    {
                        HtmlNodeCollection Ship = doc.DocumentNode.SelectNodes("//*[@id=\"ctl00_ctl00_plcMain_plcMain_rptBOL_ctl00_rptContainers_ctl01_pnlContainer\"]/table[1]/tbody[1]/tr/td[1]/span");
                        HtmlNodeCollection Booking = doc.DocumentNode.SelectNodes("//*[@id=\"ctl00_ctl00_plcMain_plcMain_rptBOL_ctl00_rptContainers_ctl01_pnlContainer\"]/table[1]/tbody[2]/tr/td[2]/span");
                        // Adding results to table
                        result = "<table><tr><td>Gemi Adı </td><td>Booking No</td></tr><tr><td>";
                        result += Ship.FirstOrDefault().InnerText + "</td><td>" + Booking.FirstOrDefault().InnerText + "</td><tr>";
                    }
                }
                catch (Exception exp)
                {
                    result += "<font color=\"red\"> No Results found! </font> <br /><br />";
                }
                result += "</table>";
            }
            return result;
        }
    }

}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.