简体   繁体   English

获取具有特定属性的表的HTML源代码

[英]Get HTML source code of table with specific attribute

I am trying to get the HTML source code of a table with a specific attribute. 我正在尝试获取具有特定属性的表的HTML源代码。 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. 当我执行http请求以获取源代码时,它将自动为我提供标准GMT,即GMT -5:00。

How can I get the source for example with GMT 0:00? 我如何获得格林尼治标准时间0:00等来源?

With HTML Agility Pack, you can use the following extension method to get a specific element with a specific attribute: 借助HTML Agility Pack,您可以使用以下扩展方法来获取具有特定属性的特定元素:

    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: 例如,如果要查找类为 “ gmt0”的 ,则可以这样调用扩展方法:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM