想要从网站上提取数据填充字典,但我在添加它们时失败了。

huangapple go评论57阅读模式
英文:

Wants to populate dictionary with data from website, but I fail on adding them

问题

我正在尝试创建一个方法,以从密码保护的网页上获取数据。我使用了Selenium Web Driver来成功连接到网站并登录,但我想将数据存储到我创建的字典中。

// 自定义参数
public class Params_Rezervation
{
    public string Time { get; set; } 
    public string Guests { get; set; } 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public string Status { get; set; } 
    public string Text { get; set; } 
    public string Table { get, set; } 
}

这是我想在我的字典中拥有的自定义参数,以便我可以访问它们。我尝试创建这个字典。

```csharp
public static Dictionary<int, Params_Rezervation> rezervace_data = new Dictionary<int, Params_Rezervation>();

然后,我尝试从网站中填充它,但在将数据添加到字典时失败。

public static void LoadReservation()
{
    // 设置Chrome参数
    var chromeOptions = new ChromeOptions();
    chromeOptions.AddArguments("--headless");

    // 隐藏控制台
    var chromeDriverService = ChromeDriverService.CreateDefaultService();
    chromeDriverService.HideCommandPromptWindow = true;

    try
    {
        using (var browser = new ChromeDriver(chromeDriverService, chromeOptions))
        {
            // 设置网站
            browser.Navigate().GoToUrl("specific website");

            // 输入密码 + 提交
            browser.FindElement(By.Id("pwbox-580")).SendKeys("password");
            browser.FindElement(By.Name("Submit")).Click();

            // 查找特定表格
            IWebElement tableElement = browser.FindElement(By.XPath("//*[@id=\"post-580\"]/div/div[2]/table"));

            // 查找行
            IList<IWebElement> rows = tableElement.FindElements(By.XPath(".//tr"));

            // 数据
            int number = 0;

            foreach (IWebElement row in rows)
            {
                // 查找特定列
                IList<IWebElement> columns = row.FindElements(By.XPath(".//th"));

                // 错误行,我不知道如何继续
                rezervace_data.Add(number, new Params_Rezervation());

                // 数据填充
                rezervace_data[number].Time = columns[0].Text;
                rezervace_data[number].Guests = columns[1].Text;
                rezervace_data[number].Name = columns[2].Text;
                rezervace_data[number].Email = columns[3].Text;
                rezervace_data[number].Text = columns[4].Text;
                rezervace_data[number].Table = columns[5].Text;

                number++;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("加载数据时发生错误!\n" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

我得到的错误:
System.NullReferenceException:“对象引用未设置为对象的实例。”
System.Collections.Generic.Dictionary<TKey, TValue>.this[TKey].get返回null。

我正在尝试学习编程,所以如果有人告诉我需要更改什么,将非常有帮助,因为我是通过自学和网站学习的。

英文:

I am trying to make method where I could scrap data from webpage that's behind password locked form. I used Selenium Web Driver to successfully connect to website and login there, but I would like to store the data do Dicitonary that I made.

// Custom params

public class Params_Rezervation
{
    public string Time { get; set; } 
    public string Guests { get; set; } 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public string Status { get; set; } 
    public string Text { get; set; } 
    public string Table { get; set; } 
}

This is the custom params I would like to have in my dicitonary so I could access them. I tried to make this dictionary.

public static Dictionary&lt;int, Params_Rezervace&gt; rezervace_data = new Dictionary&lt;int, Params_Rezervace&gt;();

Then I tried it populate it with data from website, but I fail on adding data to dictionary.

public static void LoadReservation()
    {
        // Setting up a 
        var chromeOptions = new ChromeOptions();
        chromeOptions.AddArguments(&quot;--headless&quot;);

        // Console hiding
        var chromeDriverService = ChromeDriverService.CreateDefaultService();
        chromeDriverService.HideCommandPromptWindow = true;

        try
        {
            using (var browser = new ChromeDriver(chromeDriverService, chromeOptions))
            {
                // Setting up a website 
                browser.Navigate().GoToUrl(&quot;specific website&quot;);

                // Entering password + submiting it
                browser.FindElement(By.Id(&quot;pwbox-580&quot;)).SendKeys(&quot;password&quot;);
                browser.FindElement(By.Name(&quot;Submit&quot;)).Click();

                // Finding the specific table
                IWebElement tableElement = browser.FindElement(By.XPath(&quot;//*[@id=\&quot;post-580\&quot;]/div/div[2]/table&quot;));

                // Finding the row
                IList&lt;IWebElement&gt; rows = tableElement.FindElements(By.XPath(&quot;.//tr&quot;));

                // Data
                int number = 0;

                foreach (IWebElement row in rows)
                {
                    // Finding specific columns
                    IList&lt;IWebElement&gt; columns = row.FindElements(By.XPath(&quot;.//th&quot;));

                    //The error line where I dont know how to continue 
                    rezervace_data.Add(number, Params_Rezervace);

                    // Data populating
                    rezervace_data[number].Time = columns[0].Text;
                    rezervace_data[number].Guests = columns[1].Text;
                    rezervace_data[number].Name = columns[2].Text;
                    rezervace_data[number].Email = columns[3].Text;
                    rezervace_data[number].Text = columns[4].Text;
                    rezervace_data[number].Table = columns[5].Text;

                    number++;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(&quot;Error while loading data! \n&quot; + ex, &quot;Error&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

Error I get:
System.NullReferenceException: 'The object reference is not set to an instance of the object.'

System.Collections.Generic.Dictionary<TKey, TValue>.this[TKey].get returned null.

I trying to learn programming so If someone will answer me what to change it will be very helpfull, because I am learning it by myself and websites 想要从网站上提取数据填充字典,但我在添加它们时失败了。

答案1

得分: 0

你的“错误行”应该是这样的:

rezervace_data.Add(number, new Params_Rezervace());

你需要在类型为 Params_Rezervace 的字典中添加一个对象,而你可以使用 new 关键词来实现这个操作。

英文:

Your “error line” should be something like this:

rezervace_data.Add(number, new Params_Rezervace());

You need to add an object in your dictionary of type Params_Rezervace, and the way you do that is with the new keyword.

huangapple
  • 本文由 发表于 2023年2月19日 22:55:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定