如何访问我抓取的值并将其添加到我的 DataGrid?请看截图

Posted

技术标签:

【中文标题】如何访问我抓取的值并将其添加到我的 DataGrid?请看截图【英文标题】:How can i access to the Value that i scraped and Add it to my DataGrid? Pls Look at the screenshot 【发布时间】:2021-08-06 15:06:46 【问题描述】:
public void ScrapeData(string page)
        
            var web = new htmlWeb();
            var doc = web.Load(page);

            var Articles = doc.DocumentNode.SelectNodes("//*[@class = 'b-product-grid-tile js-tile-container']");
          

 


            foreach (var article in Articles)
            
                var Sneaker = HttpUtility.HtmlDecode(article.SelectSingleNode(".//span[@class ='b-product-tile-link js-product-tile-link']").InnerText);
                var Preis = HttpUtility.HtmlDecode(article.SelectSingleNode(".//div[@class ='b-product-tile-price']").InnerText);
                var hrefList = doc.DocumentNode.SelectNodes("//a").Select(p => p.GetAttributeValue("href", "not found"));
                Debug.Print(Sneaker + Preis + hrefList);

                _entries.Add(new EntryModel  Products = Sneaker, Preis = Preis, Link = hrefList ); // can´t convert string implicitly (Sory i have Visual Studio in German i try to translate the Error) 
                
            

All the Links 但是我怎样才能访问它们呢?并将它们放在我的 DataGrid 中

我找到了链接,但不知道如何访问它们并将其放入我的 DataGrid

添加 .ToArray 后,调试器打印总是遇到同样的问题 (System.Linq.Enumerable+WhereSelectEnumerableIterator`2[HtmlAgilityPack.HtmlNode,System.String])

这里是编辑后代码的一些截图

Same Problem

The Edit Code

再次对我提出问题的方式感到非常抱歉,我是新手,如果你能纠正我,我很高兴

【问题讨论】:

【参考方案1】:

不完全清楚你想要哪个链接;你只突出了一个。但是,如果你这样做:

var hrefList = doc.DocumentNode.SelectNodes("//a")
  .Select(p => p.GetAttributeValue("href", "not found"))
  .ToArray();

(添加 .ToArray())

然后您可以访问例如第二个通过hrefList[1]

如果你只想要第二个,你最好这样做:

 var href = doc.DocumentNode.SelectNodes("//a")
   .Select(p => p.GetAttributeValue("href", "not found"))
   .Skip(1)
   .First();
            

如果只需要一个,则无需将全部复制到数组中

【讨论】:

欢迎快速回答,但如果我 Debug.Print(hrefList) 我只是得到 (System.Linq.Enumerable+WhereSelectEnumerableIterator`2[HtmlAgilityPack.HtmlNode,System.String]) 对错误的高亮链接表示歉意。我要访问的第一个链接是 [44] 并且总是跳过 3 个链接(公元前有 3 次相同的链接)。 如果我 Debug.Print(hrefList) 我刚刚得到... - 您是否按照我的建议添加了 ToArray() ?如果你调用 ToArray,那么 Debug.Print(hrefList) 应该告诉你它是一个字符串数组,而不是 WhereSelectEnumerableIterator 然后当你把它作为一个数组时,你可以访问索引 44,然后是 47 或 48(我无法理解“跳过 3,因为有 3 个相同” - 1 个索引是错误的 - 如果有 3 个相同,你想要第一个但不是其他 2,你在索引 44 处取第一个,跳过 45 和 46,下一个你想要的是 47。如果有 4 个相同,你取 48。如果你不想要 44 然后直接去 47/48.. 不管你想要什么,从数组中得到你想要的很简单,但你必须先ToArray() 我编辑了我的问题并添加了一些截图,如果你有时间你可以看看吗?

以上是关于如何访问我抓取的值并将其添加到我的 DataGrid?请看截图的主要内容,如果未能解决你的问题,请参考以下文章

从 Pixel Monitor 抓取数据

如何使用延迟python代码更慢地抓取[重复]

如何使用 Google API 进行离线访问

如何保留我的距离表示分数字段并将其映射到我的结果实体中?

如何将提及变量转换为普通变量并将其打印到我的终端

如何从文本框中获取数据并将其添加到 dataTable?