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

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

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

问题

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

  1. // 自定义参数
  2. public class Params_Rezervation
  3. {
  4. public string Time { get; set; }
  5. public string Guests { get; set; }
  6. public string Name { get; set; }
  7. public string Email { get; set; }
  8. public string Status { get; set; }
  9. public string Text { get; set; }
  10. public string Table { get, set; }
  11. }
  12. 这是我想在我的字典中拥有的自定义参数,以便我可以访问它们。我尝试创建这个字典。
  13. ```csharp
  14. public static Dictionary<int, Params_Rezervation> rezervace_data = new Dictionary<int, Params_Rezervation>();

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

  1. public static void LoadReservation()
  2. {
  3. // 设置Chrome参数
  4. var chromeOptions = new ChromeOptions();
  5. chromeOptions.AddArguments("--headless");
  6. // 隐藏控制台
  7. var chromeDriverService = ChromeDriverService.CreateDefaultService();
  8. chromeDriverService.HideCommandPromptWindow = true;
  9. try
  10. {
  11. using (var browser = new ChromeDriver(chromeDriverService, chromeOptions))
  12. {
  13. // 设置网站
  14. browser.Navigate().GoToUrl("specific website");
  15. // 输入密码 + 提交
  16. browser.FindElement(By.Id("pwbox-580")).SendKeys("password");
  17. browser.FindElement(By.Name("Submit")).Click();
  18. // 查找特定表格
  19. IWebElement tableElement = browser.FindElement(By.XPath("//*[@id=\"post-580\"]/div/div[2]/table"));
  20. // 查找行
  21. IList<IWebElement> rows = tableElement.FindElements(By.XPath(".//tr"));
  22. // 数据
  23. int number = 0;
  24. foreach (IWebElement row in rows)
  25. {
  26. // 查找特定列
  27. IList<IWebElement> columns = row.FindElements(By.XPath(".//th"));
  28. // 错误行,我不知道如何继续
  29. rezervace_data.Add(number, new Params_Rezervation());
  30. // 数据填充
  31. rezervace_data[number].Time = columns[0].Text;
  32. rezervace_data[number].Guests = columns[1].Text;
  33. rezervace_data[number].Name = columns[2].Text;
  34. rezervace_data[number].Email = columns[3].Text;
  35. rezervace_data[number].Text = columns[4].Text;
  36. rezervace_data[number].Table = columns[5].Text;
  37. number++;
  38. }
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. MessageBox.Show("加载数据时发生错误!\n" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
  44. }
  45. }

我得到的错误:
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

  1. public class Params_Rezervation
  2. {
  3. public string Time { get; set; }
  4. public string Guests { get; set; }
  5. public string Name { get; set; }
  6. public string Email { get; set; }
  7. public string Status { get; set; }
  8. public string Text { get; set; }
  9. public string Table { get; set; }
  10. }

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

  1. 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.

  1. public static void LoadReservation()
  2. {
  3. // Setting up a
  4. var chromeOptions = new ChromeOptions();
  5. chromeOptions.AddArguments(&quot;--headless&quot;);
  6. // Console hiding
  7. var chromeDriverService = ChromeDriverService.CreateDefaultService();
  8. chromeDriverService.HideCommandPromptWindow = true;
  9. try
  10. {
  11. using (var browser = new ChromeDriver(chromeDriverService, chromeOptions))
  12. {
  13. // Setting up a website
  14. browser.Navigate().GoToUrl(&quot;specific website&quot;);
  15. // Entering password + submiting it
  16. browser.FindElement(By.Id(&quot;pwbox-580&quot;)).SendKeys(&quot;password&quot;);
  17. browser.FindElement(By.Name(&quot;Submit&quot;)).Click();
  18. // Finding the specific table
  19. IWebElement tableElement = browser.FindElement(By.XPath(&quot;//*[@id=\&quot;post-580\&quot;]/div/div[2]/table&quot;));
  20. // Finding the row
  21. IList&lt;IWebElement&gt; rows = tableElement.FindElements(By.XPath(&quot;.//tr&quot;));
  22. // Data
  23. int number = 0;
  24. foreach (IWebElement row in rows)
  25. {
  26. // Finding specific columns
  27. IList&lt;IWebElement&gt; columns = row.FindElements(By.XPath(&quot;.//th&quot;));
  28. //The error line where I dont know how to continue
  29. rezervace_data.Add(number, Params_Rezervace);
  30. // Data populating
  31. rezervace_data[number].Time = columns[0].Text;
  32. rezervace_data[number].Guests = columns[1].Text;
  33. rezervace_data[number].Name = columns[2].Text;
  34. rezervace_data[number].Email = columns[3].Text;
  35. rezervace_data[number].Text = columns[4].Text;
  36. rezervace_data[number].Table = columns[5].Text;
  37. number++;
  38. }
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. MessageBox.Show(&quot;Error while loading data! \n&quot; + ex, &quot;Error&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information);
  44. }

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:

确定