本文整理汇总了C#中HtmlAgilityPack.HtmlDocument类的典型用法代码示例。如果您正苦于以下问题:C# HtmlDocument类的具体用法?C# HtmlDocument怎么用?C# HtmlDocument使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HtmlDocument类属于HtmlAgilityPack命名空间,在下文中一共展示了HtmlDocument类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParsePage
public static SteamApp ParsePage(int appId, string html)
{
if (string.IsNullOrWhiteSpace(html)) throw new ArgumentNullException(nameof(html));
try
{
var app = SteamApp.NewSteamApp(appId, html);
var htmlDocument = new HtmlDocument();
var htmlCleaned = html.Replace("\"", "'");
htmlDocument.LoadHtml(htmlCleaned);
var documentNode = htmlDocument.DocumentNode;
var titleNode = documentNode.SelectSingleNode($"//div[@class='{AppTitleClass}']");
app.Title = titleNode.InnerHtml.Trim();
var packageNodes = documentNode.SelectNodes($"//div[@class='{PackageClass}']").ToArray();
foreach (var packageNode in packageNodes)
{
AddPackage(app, packageNode);
}
return app;
}
catch (Exception)
{
throw new InvalidAppException(appId);
}
}
示例2: RefreshAsync
/// <summary>
/// 非同步刷新最新資訊
/// </summary>
/// <returns></returns>
public async Task RefreshAsync() {
HttpClient client = new HttpClient();
HtmlDocument HTMLDoc = new HtmlDocument();
HTMLDoc.LoadHtml(await client.GetStringAsync(DataSource));
var script = HTMLDoc.DocumentNode.Descendants("script")
.Where(x => x.InnerHtml?.Length > 0).Select(x => x.InnerHtml).ToArray();
var tempAry = script.First()
.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select((x, i) => new { index = i, item = x })
.GroupBy(x => Math.Floor(x.index / 4.0));
this.LastPassed = null;
this.Delay = new TimeSpan();
foreach (var item in tempAry) {
string[] temp = item.Select(x=>x.item).ToArray();
if(temp[3] == "TRSearchResult.push('x')") {
this.LastPassed = await Station.GetStationByNameAsync(
innerString(temp[0],"'","'")
);
}
}
var time = new TimeSpan(0, int.Parse(innerString(script.Last(), "=", ";")),0);
this.Delay= time;
}
示例3: ParseHtmlAsync
public async Task<string> ParseHtmlAsync(string tenant, string html)
{
var doc = new HtmlDocument();
doc.LoadHtml(html);
var nodes = doc.DocumentNode.SelectNodes("//include[@article-alias and @category-alias]");
if (nodes == null)
{
return html;
}
foreach (var node in nodes)
{
string alias = node.Attributes["article-alias"].Value;
string categoryAlias = node.Attributes["category-alias"].Value;
var model = await ContentModel.GetContentAsync(tenant, categoryAlias, alias).ConfigureAwait(false);
if (model != null)
{
string contents = model.Contents;
var newNode = HtmlNode.CreateNode(contents);
node.ParentNode.ReplaceChild(newNode, node);
}
}
return doc.DocumentNode.OuterHtml;
}
示例4: ReplaceTags
private string ReplaceTags(string html)
{
var doc = new HtmlDocument();
doc.LoadHtml(html);
var divs = doc.DocumentNode.Descendants("div").Where(d =>
d.Attributes.Contains("class")).ToList(); //&& d.Attributes["class"].Value.Contains("editable-wrapper"));
var editableDivs = new List<HtmlNode>();
foreach (var div in divs)
{
if(div.Attributes["class"].Value.Contains("editable-wrapper"))
{
editableDivs.Add(div);
}
}
foreach (var editableDiv in editableDivs)
{
editableDiv.AppendChild(GetEditButtonElement());
editableDiv.AppendChild(GetSaveButtonElement());
}
return doc.DocumentNode.OuterHtml;
}
示例5: datascraper
public datascraper()
{
string url = @"http://www.bbc.co.uk/sport/football/results/partial/competition-118996114";
HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument doc = new HtmlDocument{ OptionUseIdAttribute = true };
doc = htmlWeb.Load(url);
HtmlNodeCollection mtchrslts = doc.DocumentNode.SelectNodes("//tr[@id]");
string date;
string ateam;
string hteam;
string score;
string idmess;
string idnum;
string[] teamscores;
string teamscoreh;
string teamscorea;
foreach (HtmlNode matchresult in mtchrslts)
{
idmess = matchresult.SelectSingleNode("//tr[@id]").Id;
idnum = idmess.Replace("match-row-", "");
score = matchresult.SelectSingleNode("//abbr[@title='Score']").InnerText;
teamscores = score.Split('-');
teamscoreh = teamscores[0];
teamscorea = teamscores[1];
hteam = matchresult.SelectSingleNode("//p[(@class='team-home teams')]").InnerText;
ateam = matchresult.SelectSingleNode("//p[(@class='team-away teams')]").InnerText;
date = matchresult.SelectSingleNode("//td[(@class='match-date')]").InnerText;
}
return;
}
示例6: GetLinkFromWebPage
private async Task<String> GetLinkFromWebPage()
{
WebRequest request = WebRequest.CreateHttp(ZERANOE_WEB);
using (WebResponse response = await request.GetResponseAsync())
using (Stream s = response.GetResponseStream())
{
HtmlDocument doc = new HtmlDocument();
doc.Load(s);
HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("/html/body/div[@id='builds-page']/div[@class='grid-460']/a[@class='latest']");
int bits = Environment.Is64BitProcess ? 64 : 32;
Regex regex = new Regex($"Download FFmpeg git-\\S+ {bits}-bit Static");
foreach (var item in collection)
{
if (regex.IsMatch(item.InnerText))
{
string link = item.GetAttributeValue("href", null);
if (link != null)
{
// Link is not absolute (./win64/ffmpeg.7z)
if (link.StartsWith("."))
link = ZERANOE_WEB + link.Substring(2);
return link;
}
}
}
}
return null;
}
示例7: LoadHtmlTemplate
private void LoadHtmlTemplate()
{
this.document = new HtmlDocument();
this.document.Load( Assembly.GetExecutingAssembly().GetManifestResourceStream(HtmlTemplate) );
this.table = new HtmlNodeCollection(document.GetElementbyId(LogEventTableId));
}
示例8: Parse
protected override void Parse(String page)
{
var doc = new HtmlDocument();
doc.LoadHtml(page);
var nodes = doc.DocumentNode.SelectNodes("//ul[@class='list']//li");
if (nodes != null)
{
foreach (var node in nodes)
{
var sale = false; //из списка нельзя получить инфу о скидке, поэтому всегда false
var title = node.SelectSingleNode(".//div[@class='title']").FirstChild.InnerText.Replace("\r\n", " ");
var searchString = DelBadChar(ref title);
var gameUrl = StoreUrl + node.SelectSingleNode(".//a").GetAttributeValue("href", String.Empty);
var cost = node.SelectSingleNode(".//div[@class='price']//span[@class='new']").InnerText;
_entries.Add(new GameEntry()
{
SearchString = searchString,
StoreUrl = StoreUrl,
Title = title,
GameUrl = gameUrl,
Cost = cost,
Sale = sale
});
}
}
}
示例9: HeroDataConverter
public HeroDataConverter()
{
agilityPackHelper = new AgilityPackHelper();
HtmlDocument doc = new HtmlDocument();
heroesName = GetHeroesName();
foreach (var heroName in heroesName)
{
doc = LoadHeroHtmlPage(heroName);
skillImages = GetSkillPortraits(doc);
skillNames = GetSkillNames(doc);
skillDescriptions = GetSkillDescriptions(doc);
primaryStatsImages = GetPrimaryStatsImages(doc);
primaryStatsValues = GetPrimaryStatsValues(doc);
biography = GetBiography(doc).Trim() ;
manaCostDictionary = GetManaCost(doc);
coolDownList = GetCoolDown(doc);
abilityCastType = GetAbilityCastType(doc);
skillTargetAffectedType = GetSkillTargetAffectedType(doc);
skillDamageType = GetSkillDamageType(doc);
skillVideo = GetSkillVideo(doc);
skillRemainingValues = GetSkillRemainingValues(doc);
heroCreator.createHero(heroName, biography);
for (int i = 0; i < skillNames.Count; i++)
{
heroCreator.createHeroSkill(skillNames[i], skillDescriptions[i], manaCostDictionary, coolDownList, abilityCastType[i],
skillTargetAffectedType[i], skillDamageType[i], skillVideo);
}
}
}
示例10: GetFundamentals
static IDictionary<string, string> GetFundamentals(Uri url)
{
string html;
using (var webClient = new WebClient())
html = webClient.DownloadString(url);
var doc = new HtmlDocument
{
OptionFixNestedTags = true
};
doc.LoadHtml(html);
var fundamentals = new Dictionary<string, string>();
var trs = doc.DocumentNode.SelectNodes("//div[@data-ajax-name='EquitySummaryTable']//table[contains(@class, 'horizontalTable')]//tr");
if (trs == null)
return fundamentals;
foreach (var tr in trs)
{
var name = tr.Elements("th").Single().InnerText;
var value = tr.Elements("td").Single().InnerText;
if (value == "--")
{
Log.DebugFormat("Missing: {0} = {1}", name, value);
continue;
}
Log.DebugFormat("Found: {0} = {1}", name, value);
fundamentals.Add(name, value);
}
return fundamentals;
}
示例11: GetPageCount
//Получаем общее число страниц
protected override int GetPageCount(HtmlDocument document, string startAddress)
{
HtmlNodeCollection c = document.DocumentNode.SelectNodes("//div[@class='pageNavBar']");
int pageCount = 1;
if (c != null)
foreach (HtmlNode n in c)
{
HtmlNodeCollection cc = n.ChildNodes;
if (cc != null)
{
int i = 0;
foreach (HtmlNode nn in cc)
{
i++;
if (i == 6)
{
pageCount = int.Parse(Regex.Match(nn.InnerText, @"(\d+)").Groups[1].Value);
}
}
}
break;
}
return pageCount;
}
示例12: ParseTopPerson
public static void ParseTopPerson(string html, Action<List<Person>> finished)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler((sender, e) =>
{
List<Person> TopPerson = new List<Person>();
HtmlDocument hDoc = new HtmlDocument();
hDoc.LoadHtml(html);
var tableRows = hDoc.DocumentNode.SelectNodes(Constants.Instance.XPATH_GAME_TOP_RESULT);
foreach (var node in tableRows)
{
var results = ChildElementsInTableRow(node);
if (results.Count == Constants.Instance.COUNT_GAME_TOP_RESULT_COLUMNS)
{
var person = TopPersonFromStrings(results);
if (person.Total != 0)
{
TopPerson.Add(person);
}
}
}
finished(TopPerson);
});
bw.RunWorkerAsync();
}
示例13: ParseRecommends
public static void ParseRecommends(string html, Action<List<Recommend>> finished)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler((sender, e) =>
{
List<Recommend> allRecommends = new List<Recommend>();
HtmlDocument hDoc = new HtmlDocument();
hDoc.LoadHtml(html);
var tableRows = hDoc.DocumentNode.SelectNodes(Constants.Instance.XPATH_GAME_SHOW_RESULT);
foreach (var node in tableRows)
{
var results = ChildElementsInTableRow(node);
if (results.Count == Constants.Instance.COUNT_GAME_SHOW_RESULT_COLUMNS)
{
var rec = RecommendFromStrings(results);
if (IsValidRecommend(rec))
{
allRecommends.Add(rec);
}
}
}
finished(allRecommends);
});
bw.RunWorkerAsync();
}
示例14: ConvertHtml
private void ConvertHtml(ExCSS.Stylesheet sheet, string html, Section section)
{
_sheet = sheet;
if (string.IsNullOrEmpty(html))
{
throw new ArgumentNullException("html");
}
if (section == null)
{
throw new ArgumentNullException("section");
}
//section.PageSetup.HeaderDistance = "0.001cm";
section.PageSetup.FooterDistance = Unit.FromCentimeter(0.01);
// Create a paragraph with centered page number. See definition of style "Footer".
var footer = section.Footers.Primary.AddParagraph();
//section.Footers.Primary.
footer.Format.Alignment = ParagraphAlignment.Right;
footer.AddPageField();
footer.AddText(" of ");
footer.AddNumPagesField();
var doc = new HtmlDocument();
doc.LoadHtml(html);
ConvertHtmlNodes(doc.DocumentNode.ChildNodes, sheet, section);
}
示例15: btnTestCode_Click
private void btnTestCode_Click(object sender, RoutedEventArgs e)
{
var mainPage = GetHtml("http://htmlagilitypack.codeplex.com");
var homepage = new HtmlDocument();
homepage.LoadHtml(mainPage);
var nodes =
homepage.DocumentNode.Descendants("a").Where(x => x.Id.ToLower().Contains("releasestab")).FirstOrDefault
();
var link = nodes.Attributes["href"].Value;
var dc = new HtmlDocument();
try
{
Cursor = Cursors.Wait;
var req = (HttpWebRequest) WebRequest.Create(link);
using (var resp = req.GetResponse().GetResponseStream())
using (var read = new StreamReader(resp))
{
dc.LoadHtml(read.ReadToEnd());
var span =
dc.DocumentNode.Descendants("span").Where(
x => x.Id.ToLower().Contains("releasedownloadsliteral")).FirstOrDefault();
MessageBox.Show(
int.Parse(span.InnerHtml.ToLower().Replace("downloads", string.Empty).Trim()).ToString());
}
}
catch (Exception ex)
{
MessageBox.Show("Error loading file: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error,
MessageBoxResult.OK);
}
}