Home > Net >  My C# WPF webscraper returns error when more than one result is found
My C# WPF webscraper returns error when more than one result is found

Time:05-25

I am working on a WPF XAML application that scrapes certain websites for products. I have the search part working and it finds what I'm looking for. But as soon as there is more then 1 result I get a System.InvalidoperationException. I use a ObservableCollection to put the results into a <ListBox>.

Here is the search method:

private static ObservableCollection<EntryModel> _entries = new ObservableCollection<EntryModel>();

public static ObservableCollection<EntryModel> LoadCollectionData
{
    get { return _entries; }
    set { _entries = value; }
}

public static void PrehmSearchResults(string SearchQuery)
{
    HtmlWeb web = new HtmlWeb();

    try
    {
        string ZoekOpdracht = SearchQuery.Replace(" ", " ");
        HtmlDocument doc = web.Load("https://www.prehmshop.de/advanced_search_result.php?keywords="   ZoekOpdracht);

        var title = doc.DocumentNode.CssSelect("div.header_cell > a").Single().InnerText;

        var links = doc.DocumentNode.CssSelect("a.product_link");
        var productLink = new List<string>();
        var productTitle = new List<string>();

        foreach (var item in links)
        {
            if (item.Attributes["href"].Value.Contains(".html"))
            {
                productLink.Add(item.Attributes["href"].Value);
                productTitle.Add(title);
            }
        }

        var TitleAndLink = productLink.Zip(productTitle, (l, t) => new { productLink = l, productTitle = t });
        
        foreach (var nw in TitleAndLink)
        {
            var product = new List<EntryModel>();
            var adDetails = new EntryModel
            {
                Title = nw.productTitle,
                Link = nw.productLink
            };

            Debug.Print(adDetails.ToString());
            var ZoekOpdrachtInTitle = adDetails.Title.ToLower().Contains(ZoekOpdracht.ToLower());
            
            if (ZoekOpdrachtInTitle)
            {
                _entries.Add(adDetails);
            }
        }
    }

CodePudding user response:

So I found the solution without changing too much code. thanks for the help from @PaulSinnema.

The link is part of the title so I only had to change

var title = doc.DocumentNode.CssSelect("div.header_cell > a").ToList();

And I had to change the foreach loop:

foreach (var item in title)
{
    if (item.Attributes["href"].Value.Contains(".html"))
        {
            productLink.Add(item.Attributes["href"].Value);
            productTitle.Add(item.InnerText);
        }
}
  • Related