简体   繁体   English

从网页获取信息

[英]Getting Information From Webpage

I'm not really a programmer (more of a scripter) and wrote a very quick script using AutoIt! 我不是一个真正的程序员(更像是一名脚本编写者),并且使用AutoIt编写了非常快速的脚本! to gather information from HP's website on a listing of computers we have. 在我们拥有的计算机列表中从HP网站收集信息。 I thought I would try to port it over to C# to make it a little more "professional". 我以为我会尝试将其移植到C#上,使其更加“专业”。

The code reads in a file containg product code, serial number. 该代码读取包含产品代码,序列号的文件。 It then puts this information into a datagridview. 然后将这些信息放入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! 在AutoIt中! it was frame 19 and then I parsed that. 那是第19帧,然后我解析了该帧。 I just don't quite know how to do it in C#. 我只是不太了解如何在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";

    }`

我真的帮不了您(因为我从没做过现在的工作),但是我认为watin自动化库可以为您提供很大帮助

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. 在弄清楚网页的加载之后,我已经知道应该看一下第19帧了。 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++;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM