带有 IF 语句的 C# HTML Agility Pack

发布于 2024-12-15 22:47:06 字数 2051 浏览 0 评论 0原文

我有这段代码,我需要运行 if 语句来设置变量的值。问题是当第一个条件为空时它会失败。谁能告诉我我做错了什么?

IF 语句位于 foreach 循环内部,该循环在每次迭代时向列表添加一个值。

非常感谢!

 string result = string.Empty;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";

        using (var stream = request.GetResponse().GetResponseStream())
        using (var reader = new StreamReader(stream, Encoding.UTF8))
        {
            result = reader.ReadToEnd();
        }

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.Load(new StringReader(result));
        HtmlNode root = doc.DocumentNode;

        string itemdesc = doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']").InnerText;

        HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='resultsset']/table/tbody[@class='result']/tr");

        List<string> sellers = new List<string>();
        List<string> prices = new List<string>();

        foreach (HtmlNode node in nodes)
        {
            string seller = string.Empty;
                if(node.SelectSingleNode(".//ul[@class='sellerInformation']/img").GetAttributeValue("alt", string.Empty) != null)
                {
                    seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/img").GetAttributeValue("alt", string.Empty);
                }

                else if (node.SelectSingleNode(".//ul[@class='sellerInformation']/a/img").GetAttributeValue("alt", string.Empty) != null)
                {
                    seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/a/img").GetAttributeValue("alt", string.Empty);
                }

                else
                {
                    seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/li/div/span/a/b").InnerText;
                }


            sellers.Add(seller);
            string price = node.SelectSingleNode(".//span[@class='price']").InnerText;
            prices.Add(price);

        }

I have this code that I need to run an if statement to set the value of a variable. The problem is when the first condition is null it fails. Can anyone tell me what I am doing wrong?

The IF statement is inside the foreach loop which adds a value to a list on each iteration.

Thank you very much!

 string result = string.Empty;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";

        using (var stream = request.GetResponse().GetResponseStream())
        using (var reader = new StreamReader(stream, Encoding.UTF8))
        {
            result = reader.ReadToEnd();
        }

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.Load(new StringReader(result));
        HtmlNode root = doc.DocumentNode;

        string itemdesc = doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']").InnerText;

        HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='resultsset']/table/tbody[@class='result']/tr");

        List<string> sellers = new List<string>();
        List<string> prices = new List<string>();

        foreach (HtmlNode node in nodes)
        {
            string seller = string.Empty;
                if(node.SelectSingleNode(".//ul[@class='sellerInformation']/img").GetAttributeValue("alt", string.Empty) != null)
                {
                    seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/img").GetAttributeValue("alt", string.Empty);
                }

                else if (node.SelectSingleNode(".//ul[@class='sellerInformation']/a/img").GetAttributeValue("alt", string.Empty) != null)
                {
                    seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/a/img").GetAttributeValue("alt", string.Empty);
                }

                else
                {
                    seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/li/div/span/a/b").InnerText;
                }


            sellers.Add(seller);
            string price = node.SelectSingleNode(".//span[@class='price']").InnerText;
            prices.Add(price);

        }

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

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

发布评论

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

评论(1

柠檬心 2024-12-22 22:47:06

可能 SelectSingleNode 返回 null,因此对 GetAttributeValue 的调用是一个 null 引用问题。在检查属性之前,您需要检查 SelectSingleNode 的结果。

Probably SelectSingleNode is returning null, so the call to GetAttributeValue is a null reference problem. You will need to check the result of SelectSingleNode before checking the attribute.

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