2017-02-17 76 views
0

我想获取具有特定属性的表格的HTML源代码。下面的代码将帮助您了解更多信息。获取具有特定属性的表格的HTML源代码

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; 
      } 
     } 
    } 
} 

当我们访问here时,我们可以选择设置时间范围。 我们选择的时间表会相应地修改表格。 当我做一个http请求来获取源码时,它会自动给我格林威治标准时间GMT -5:00。

我怎样才能得到源例如格林威治标准时间0:00?

+0

正如我告诉你前一个问题中,敏捷性包不能运行JavaScript和除了HTTP单一调用HTML返回的默认值之外,没有办法获取任何数据。为了做到这一点,你需要一个无头浏览器或'WebView',就像我们在上一个答案中使用的那样。考虑使用* Awesomium *(http://www.awesomium.com/)。它是免费的,并且有一些用于无头浏览的好用工具,就像一个'WebView'独立类。 –

回答

0

随着HTML敏捷性包,您可以使用下面的扩展方法来获得一个特定的元素具有特定属性的:

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(); 

    } 

例如,如果你想找到“ GMT0",你可以调用扩展方法是这样的:

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

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

我不认为这是正确的答案。具有所需GMT时间的表格不可用。唯一可用的是标准格林威治标准时间-5:00。 显然有一个标题,必须在请求之前设置。我仍然在试图弄清楚。 –

+0

啊,在这种情况下,您必须使用Fiddler或Chrome的网络工具等功能来计算当您更改组合框选择时发生的情况。以上代码用于获取特定元素的HTML源代码。 –