问题
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