C# List and HtmlParse



请原谅我的英语,这不是我的第一语言。

第 1 类是我拥有我的财产和列表的地方类 2 是我的 html 解析器。

目前,它的工作是打开网页并找到假定的链接。

现在的问题是,我想使用Linq,另一个列表,或者你们认为最好的。按 studRet(名称 id)对结果进行排序

因此,它从"itsupporter"href"中找到的所有结果都按
它支持者
结果
结果

金融人
结果
结果

应该怎么做,什么是最佳实践,有什么好的例子可以指导我找到答案吗?

我不知道如何确保它找到的链接来自正确的 studRet。

我希望这是一个足够好的问题。

类1

private readonly List<Property> dataList = new List<Property>();
    // Get property
    internal struct Property
    {
        public readonly string url;
        public readonly string cssTag;
        public readonly string studRet;
        public readonly int id;
        public Property(string url, string cssTag, string studRet, int id)
        {
            this.url = url;
            this.cssTag = cssTag;
            this.studRet = studRet;
            this.id = id;
        }
    }
    public List<Property> GetList()
    {
        dataList.Add(new Property("http://elevplads.dk/annoncer?search=It-Supporter", "//div[@class='col-xs-12 col-sm-12 col-md-12']//a", "itsupporter", 1));
        dataList.Add(new Property("http://elevplads.dk/annoncer?stillingstype=finans", "//div[@class='col-xs-12 col-sm-12 col-md-12']//a", "finans", 2));
        return dataList;
    }

第 2 类

public readonly Jobbot Jobdata = new Jobbot();
public void HandleHtml()
    {
        List<Jobbot.Property> dataList = Jobdata.GetList();         
        HtmlWeb web = new HtmlWeb();
        foreach (var data in dataList) 
        {             
            HtmlDocument document = web.Load(data.url);
            var nodes = document.DocumentNode.SelectNodes(data.cssTag);
            if (nodes != null)
            {
                foreach (HtmlNode item in nodes)
                {
                    if (item.Attributes.Contains("href"))
                    {
                        if (item.Attributes["href"].Value.StartsWith("http://"))
                        {
                            Console.WriteLine(item.Attributes["href"].Value);
                        }
                    }
                }
            }
        }
    } // End

我不太确定我是否理解你的问题。您已经按照您希望结果集的顺序发送了 Web 请求,所以我不明白为什么您需要对它们进行排序。至于将链接映射到 studRet 属性,您可以将结果放入列表中:

public void HandleHtml()
...    
List<Tuple<string, Jobbot.Property>> results = new List<Tuple<string, Jobbot.Property>>();
...
                            if (item.Attributes["href"].Value.StartsWith("http://"))
                            {
                                results.Add(Tuple.Create(item.Attributes["href"].Value, data));
                                Console.WriteLine(item.Attributes["href"].Value);
                            }

然后,如果您确实需要对结果进行排序,则可以使用:

results.OrderBy(x => x.Item2.studRet);

但如前所述,在您的情况下,没有必要。

最新更新