Get HTML source code of table with specific attribute

流过昼夜 提交于 2019-12-12 02:24:53

问题


I am trying to get the HTML source code of a table with a specific attribute. The code below will help you understand more.

public static async Task GetCldInfos()
{
    string sURL = @"https://www.investing.com/economic-calendar/";
    using (HttpClient clientduplicate = new HttpClient())
    {
        clientduplicate.DefaultRequestHeaders.Add("User-Agent",
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)");

        using (HttpResponseMessage responseduplicate = await clientduplicate.GetAsync(sURL))
        using (HttpContent contentduplicate = responseduplicate.Content)
        {
            try
            {
                string resultduplicate = await contentduplicate.ReadAsStringAsync();

                //var websiteduplicate = new HtmlDocument();
                //websiteduplicate.LoadHtml(resultduplicate);
                Debug.WriteLine(resultduplicate);
            }
            catch (Exception ex1)
            {
                throw ex1.InnerException;
            }
        }
    }
}

When we visit here we have an option to set the time frame. The timeframe we chose modifies the table accordingly. When I do an http request to get the source it automatically gives me the standard GMT which is GMT -5:00.

How can I get the source for example with GMT 0:00?


回答1:


With HTML Agility Pack, you can use the following extension method to get a specific element with a specific attribute:

    public static IEnumerable<HtmlNode> GetNodesByAttr(this HtmlDocument htmlDoc, string tag, string attributeName, string attributeValue)
    {
        var allTags = htmlDoc.DocumentNode.Descendants(tag);

        return (from htmlNode in allTags
                select htmlNode.Attributes
                    into attrs
                    from attr in attrs
                    where attr.Name == attributeName && attr.Value == attributeValue
                    select attr).Select(attr => attr.OwnerNode).ToList();

    }

For example, if you want to find table with class "gmt0", you can call the extension method like this:

var websiteduplicate = new HtmlDocument();
websiteduplicate.LoadHtml(resultduplicate);

var myElement = websiteduplicate.GetNodesByAttr("table", "class", "gmt0").FirstOrDefault();


来源:https://stackoverflow.com/questions/42308108/get-html-source-code-of-table-with-specific-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!