需要产品名称和价格才能出口

时间:2019-06-25 16:13:44

标签: c#

我需要此代码来获取产品名称和价格,并将其导出到我的sqa中。

如果您看看https://parking.edinburghairport.com/并加上一些日期 在其中,我要输出产品名称和价格。

namespace PriceCheck
{
    public partial class WebCheck : Form
    {
        string url;
        Tuple<string[], decimal[]> productdata;
        DateTime adate;
        DateTime edate;
        int fduration;
        string checktype;

        string[] productdiscription;

        public WebCheck(string urlin,DateTime arrivaldate, DateTime exitdate,int duration,string checktypein)
        {
            InitializeComponent();
            url = urlin;
            adate = arrivaldate;
            edate = exitdate;
            fduration = duration+1;
            checktype = checktypein;
        }

        private void WebCheck_Load(object sender, EventArgs e)
        {
            WebBrow.Navigate(url);
            ReloadTimer.Interval = 10000;
            ReloadTimer.Start();

            //WebBrow.Navigate(url);
            //PageLoadTimer.Interval = 6000;
            //PageLoadTimer.Start();
        }

        private void PageLoadTimer_Tick(object sender, EventArgs e)
        {
            HtmlDocument htmldoc = WebBrow.Document;
            HtmlElementCollection htmlelementcoll = htmldoc.GetElementsByTagName("H2");
            HtmlElement[] htmlelement = new HtmlElement[htmlelementcoll.Count];
            string[] outerhtmlcoll = new string[htmlelementcoll.Count];
            int productcount = 0;
            int productcounter = 0;
            string[] productlistdata;

            for (int i = 0; i < htmlelementcoll.Count; i++)
            {
                if (htmlelementcoll[i].OuterHtml.Contains("Long Stay"))
                {
                    productcount++;
                }
            }

            if (productcount != 0)
            {

                productlistdata = new string[productcount];

                for (int i = 0; i < htmlelementcoll.Count; i++)
                {
                    if (htmlelementcoll[i].OuterHtml.Contains("Long Stay"))
                    {
                        productlistdata[productcounter] = htmlelementcoll[i].OuterHtml.ToString();
                        productcounter++;
                    }
                }

                productdata = CleanData(productlistdata);
                productdiscription = new string[productcount];

                for (int f = 0; f < productcount; f++)
                {
                    //Write product to SQL
                    if (checktype != "")
                    {
                        //Write to PriceCheck
                        //check if data aready in sql
                        object a = priceCheckTableTableAdapter.CheckSQL(productdata.Item1[f], DateTime.Today, DateTime.Today.AddHours(23), adate, edate);
                        if (a == null)
                        {
                            priceCheckTableTableAdapter.Insert(DateTime.Now, adate, edate, fduration, productdata.Item2[f], productdata.Item1[f], checktype);
                        }
                        else
                        {

                        }
                    }
                    else
                    {
                        //Write to availbility
                        productAvailibilityTableAdapter1.Insert(DateTime.Now, adate, edate, fduration-1, productdata.Item1[f]);
                    }

                }

                DialogResult = DialogResult.OK;
                Dispose();
            }
            else
            {
                DialogResult = DialogResult.Abort;
                Dispose();
            }
        }

        private Tuple<string[], decimal[]> CleanData(string[] productlist)
        {
            object[,] cleaneddata = new string[productlist.Length, productlist.Length];

            string[] productcode = new string[productlist.Length];
            string[] pricestring = new string[productlist.Length];
            decimal[] price = new decimal[productlist.Length];

            for (int i = 0; i < productlist.Length; i++)
            {
                int i1 = productlist[i].IndexOf("products-list__product__section__price") + 6;
                int i2 = productlist[i].IndexOf("products-list__product__section") + 6;

                productcode[i] = productlist[i].Substring(i1, 10);
                pricestring[i] = productlist[i].Substring(i2, 10);

                productcode[i] = Regex.Replace(productcode[i], "[@\";']", string.Empty);
                pricestring[i] = Regex.Replace(pricestring[i], "[@\";']", string.Empty);
                pricestring[i] = pricestring[i].Replace(" ", "");
            }
            return Tuple.Create(productcode, price);

        }

        private void WebCheck_ReLoad(object sender, EventArgs e)
        {
            WebBrow.Navigate("about:blank");
            PageLoadTimer.Interval = 3000;
            PageLoadTimer.Start();
        }

        private void ReloadTimer_Tick(object sender, EventArgs e)
        {
            WebBrow.Refresh();
            PageLoadTimer.Interval = 3000;
            PageLoadTimer.Start();
        }
    }
}

它仅导出部分产品名称,而我根本无法获得价格。

0 个答案:

没有答案