Search code examples
c#htmlweb-scrapinghttpclienthttpresponse

Scraping Website with C# using HTML Request Not Giving Table Data


I'm making a simple website scraper to retrieve party names of supreme court cases(this is public information) in C# like in this sample link: https://www.supremecourt.gov/search.aspx?filename=/docket/docketfiles/html/public/19-8334.html

C# Code:

private static async void GetHtmlAsync(String docket)
    {
        var url = "https://www.supremecourt.gov/search.aspx?filename=/docket/docketfiles/html/public/19-8334.html";
        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.234");
        var html = await httpClient.GetStringAsync(url);

        var htmlDocument = new HtmlAgilityPack.HtmlDocument();
        htmlDocument.LoadHtml(html);

        Console.WriteLine();

    }

The problem is that whenever I run this, it successfully gives back the whole HTML file but without the data I need which is in enclosed in the element.

In browser:

HTML From Dev Tools

Display

In Runtime:

enter image description here


Solution

  • I don't know why but you should get proper response.

    Try following you might get the answer.

    var html =  httpClient.GetAsync(url).GetAwaiter().GetResult();