从网页获取信息

发布于 2024-10-31 15:13:31 字数 1685 浏览 2 评论 0原文

我并不是真正的程序员(更多的是脚本编写者),并且使用 AutoIt 编写了一个非常快速的脚本!从 HP 网站收集有关我们拥有的计算机列表的信息。我想我应该尝试将它移植到 C# 上,以使其更“专业”一点。

该代码读取包含产品代码、序列号的文件。然后它将这些信息放入 datagridview 中。有三列,第三列是开始日期(我正在寻找的)。

以下是我到目前为止所拥有的。从这里开始,我不知道下一步该做什么。该网页确实转到下一页,但我不明白如何获取我需要的信息(第一个开始日期 - 这是保修开始的时间)。在 AutoIt!这是第 19 帧,然后我解析了它。我只是不太知道如何在 C# 中做到这一点。

任何指示将不胜感激。

    private void runner(int i)
    {
        int j = i;
        bool loadFinished = false;

        webBrowser1.DocumentCompleted += delegate { loadFinished = true; };
        webBrowser1.Navigate("http://www11.itrc.hp.com/service/ewarranty/warrantyInput.do");

        while (!loadFinished )
        {
            Thread.Sleep(100);
            Application.DoEvents();
        }

        webBrowser1.Document.GetElementById("productnumber").InnerText = dt.Rows[j][0].ToString();
        webBrowser1.Document.GetElementById("serialnumber1").InnerText = dt.Rows[j][1].ToString();

        HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("SELECT");
        foreach (HtmlElement elem in elems)
        {
            if (elem.Name.ToString() == "country")
            {
                elem.SetAttribute("value", "US");
            }
        }

        int countelement = 0;
        HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("INPUT");
        foreach (HtmlElement element in col)
        {
            if (element.Name.ToString() == "")
            {
                if (countelement == 1)
                {
                    element.InvokeMember("click");
                }
                countelement++;
            }
        }
        dt.Rows[j][2] = "done";

    }`

I'm not really a programmer (more of a scripter) and wrote a very quick script using AutoIt! to gather information from HP's website on a listing of computers we have. I thought I would try to port it over to C# to make it a little more "professional".

The code reads in a file containg product code, serial number. It then puts this information into a datagridview. There are three columns, the third being the start date (what I'm looking for).

Below is what I have so far. From here I'm lost as to what to do next. The webpage does go to the next page, but I don't understand how to get the information I need (the first start date - which is the when the warranty started). In AutoIt! it was frame 19 and then I parsed that. I just don't quite know how to do it in C#.

Any pointers would be greatly appreciated.

    private void runner(int i)
    {
        int j = i;
        bool loadFinished = false;

        webBrowser1.DocumentCompleted += delegate { loadFinished = true; };
        webBrowser1.Navigate("http://www11.itrc.hp.com/service/ewarranty/warrantyInput.do");

        while (!loadFinished )
        {
            Thread.Sleep(100);
            Application.DoEvents();
        }

        webBrowser1.Document.GetElementById("productnumber").InnerText = dt.Rows[j][0].ToString();
        webBrowser1.Document.GetElementById("serialnumber1").InnerText = dt.Rows[j][1].ToString();

        HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("SELECT");
        foreach (HtmlElement elem in elems)
        {
            if (elem.Name.ToString() == "country")
            {
                elem.SetAttribute("value", "US");
            }
        }

        int countelement = 0;
        HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("INPUT");
        foreach (HtmlElement element in col)
        {
            if (element.Name.ToString() == "")
            {
                if (countelement == 1)
                {
                    element.InvokeMember("click");
                }
                countelement++;
            }
        }
        dt.Rows[j][2] = "done";

    }`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混吃等死 2024-11-07 15:13:31

我真的帮不了你(因为我从来没有做过你现在正在做的事情)但我认为watin 自动化库可以为您提供显着帮助

I really can't help you (because of I never did what you are doing now) but I think watin automation library can help you significantly

他夏了夏天 2024-11-07 15:13:31

我想我明白了。在提交按钮上的单击事件之后,我需要暂停以等待新页面加载。尽管我看到了新网页,但程序已经转到下一个命令。查看调试中的变量,我可以看到它是旧页面。下面是我使用的代码。

当我弄清楚网页加载的情况后,我已经知道要查看的是第 19 帧。从那里开始,问题只是将字符串转换为行数组,然后循环查找第一个开始日期并提取该信息。

        private void runner(int i)
    {
        int j = i;
        bool loadFinished = false;

        webBrowser1.DocumentCompleted += delegate { loadFinished = true; };
        webBrowser1.Navigate("http://www11.itrc.hp.com/service/ewarranty/warrantyInput.do");

        while (!loadFinished )
        {
            Thread.Sleep(100);
            Application.DoEvents();
        }

        webBrowser1.Document.GetElementById("productnumber").InnerText = dt.Rows[j][0].ToString();
        webBrowser1.Document.GetElementById("serialnumber1").InnerText = dt.Rows[j][1].ToString();

        HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("SELECT");
        foreach (HtmlElement elem in elems)
        {
            if (elem.Name.ToString() == "country")
            {
                elem.SetAttribute("value", "US");
            }
        }

        int countelement = 0;
        HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("INPUT");
        foreach (HtmlElement element in col)
        {
            if (element.Name.ToString() == "")
            {
                if (countelement == 1)
                {
                    element.InvokeMember("click");
                    do
                    {
                        Application.DoEvents();
                    } while (webBrowser1.IsBusy);
                }
                countelement++;
            }
        }

        string output = "";
        int county = 0;
        HtmlElementCollection elly = webBrowser1.Document.GetElementsByTagName("TABLE");
        foreach (HtmlElement el in elly)
        {
            if (county == 19)
            {
                string[] lines = el.InnerText.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (string line in lines)
                {
                    if (line.IndexOf("Start Date") != -1)
                    {
                        output = line.ToString();
                        dt.Rows[j][2] = output.Remove(0, 10);
                        break;
                    }

                }

            }
            county++;
        }

    }

I think I figured it out. After the click event on the submit button, I needed to put a pause in to wait for the new page to load. Even though I saw the new webpage, the program had already moved on to the next commands. Looking at the variables in the debug I could see it was the old page. Below is the code that I used.

After I figured out about the webpage loading, I already knew it was frame 19 to look at. From there it was just a question of converting the string into an array of lines and then looping through looking for the first Start Date and pulling that information.

        private void runner(int i)
    {
        int j = i;
        bool loadFinished = false;

        webBrowser1.DocumentCompleted += delegate { loadFinished = true; };
        webBrowser1.Navigate("http://www11.itrc.hp.com/service/ewarranty/warrantyInput.do");

        while (!loadFinished )
        {
            Thread.Sleep(100);
            Application.DoEvents();
        }

        webBrowser1.Document.GetElementById("productnumber").InnerText = dt.Rows[j][0].ToString();
        webBrowser1.Document.GetElementById("serialnumber1").InnerText = dt.Rows[j][1].ToString();

        HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("SELECT");
        foreach (HtmlElement elem in elems)
        {
            if (elem.Name.ToString() == "country")
            {
                elem.SetAttribute("value", "US");
            }
        }

        int countelement = 0;
        HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("INPUT");
        foreach (HtmlElement element in col)
        {
            if (element.Name.ToString() == "")
            {
                if (countelement == 1)
                {
                    element.InvokeMember("click");
                    do
                    {
                        Application.DoEvents();
                    } while (webBrowser1.IsBusy);
                }
                countelement++;
            }
        }

        string output = "";
        int county = 0;
        HtmlElementCollection elly = webBrowser1.Document.GetElementsByTagName("TABLE");
        foreach (HtmlElement el in elly)
        {
            if (county == 19)
            {
                string[] lines = el.InnerText.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (string line in lines)
                {
                    if (line.IndexOf("Start Date") != -1)
                    {
                        output = line.ToString();
                        dt.Rows[j][2] = output.Remove(0, 10);
                        break;
                    }

                }

            }
            county++;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文