当前位置: 首页 > news >正文

外贸网站建设推广公司软文是什么

外贸网站建设推广公司,软文是什么,做电脑网站用什么软件有哪些方面,义乌网站建设现状目录 1.安装 HtmlAgilityPack 2. 示例 HTML 3. 使用 HtmlAgilityPack 进行 HTML 解析与操作 4. 代码详解 1.加载html文档 2.选择元素 3. 提取属性 4.修改属性 5.常用的几种获取元素的 XPath 写法 HtmlAgilityPack: 轻量且高效,适合进行常规的 H…

目录

1.安装 HtmlAgilityPack

2. 示例 HTML

 3. 使用 HtmlAgilityPack 进行 HTML 解析与操作

 4. 代码详解

1.加载html文档

2.选择元素

3. 提取属性

4.修改属性 

5.常用的几种获取元素的 XPath 写法


HtmlAgilityPack:

  • 轻量且高效,适合进行常规的 HTML 解析。
  • 由于其轻量化设计,在只需简单提取或修改元素内容时,HtmlAgilityPack 会显得更快。
  • 对于层级较深或大规模的 HTML 文档,HtmlAgilityPack 也会处理得较为流畅。
  • 文件大小较小,功能单一,适用于解析 HTML 和使用 XPath 查询。
  • 没有内置对 CSS 选择器的支持,需要通过额外库扩展(如 Fizzler)。
1.安装 HtmlAgilityPack

通过 NuGet 包管理器安装 HtmlAgilityPack: 

2. 示例 HTML

假设我们有以下 HTML 内容,需要解析和操作: 

 <!DOCTYPE html><html><head><title>HtmlAgilityPack Example</title><style>.highlight { color: yellow; }#main { background-color: #f0f0f0; }</style></head><body><h1 id='main-heading' class='highlight'>Welcome to HtmlAgilityPack</h1><p>This is a <span class='highlight'>simple</span> example.</p><a href='https://example.com' target='_blank'>Visit Example.com</a><ul id='items'><li class='item'>Item 1</li><li class='item'>Item 2</li><li class='item'>Item 3</li></ul><input type='text' id='username' value='JohnDoe' /><input type='password' id='password' /></body></html>
 3. 使用 HtmlAgilityPack 进行 HTML 解析与操作

 以下是一个详细的 C# 示例,展示如何使用 HtmlAgilityPack 进行各种操作:

using HtmlAgilityPack;
using System;
using System.Linq;class Program
{static void Main(string[] args){// 示例 HTML 内容string html = @"<!DOCTYPE html><html><head><title>HtmlAgilityPack Example</title><style>.highlight { color: yellow; }#main { background-color: #f0f0f0; }</style></head><body><h1 id='main-heading' class='highlight'>Welcome to HtmlAgilityPack</h1><p>This is a <span class='highlight'>simple</span> example.</p><a href='https://example.com' target='_blank'>Visit Example.com</a><ul id='items'><li class='item'>Item 1</li><li class='item'>Item 2</li><li class='item'>Item 3</li></ul><input type='text' id='username' value='JohnDoe' /><input type='password' id='password' /></body></html>";// 1. **加载 HTML 文档**HtmlDocument document = new HtmlDocument();document.LoadHtml(html);// 2. **选择元素**// 使用 XPath 选择所有具有 class 'highlight' 的元素var highlights = document.DocumentNode.SelectNodes("//*[@class='highlight']");Console.WriteLine("Elements with class 'highlight':");foreach (var elem in highlights){Console.WriteLine($"- <{elem.Name}>: {elem.InnerText}");}// 使用 ID 选择器选择特定元素var mainHeading = document.GetElementbyId("main-heading");if (mainHeading != null){Console.WriteLine($"\nElement with ID 'main-heading': {mainHeading.InnerText}");}// 选择所有 <a> 标签var links = document.DocumentNode.SelectNodes("//a");Console.WriteLine("\nAll <a> elements:");foreach (var link in links){Console.WriteLine($"- Text: {link.InnerText}, Href: {link.GetAttributeValue("href", "")}, Target: {link.GetAttributeValue("target", "")}");}// 选择所有具有 class 'item' 的 <li> 元素var items = document.DocumentNode.SelectNodes("//li[@class='item']");Console.WriteLine("\nList items with class 'item':");foreach (var item in items){Console.WriteLine($"- {item.InnerText}");}// 选择特定类型的输入元素var textInput = document.DocumentNode.SelectSingleNode("//input[@type='text']");var passwordInput = document.DocumentNode.SelectSingleNode("//input[@type='password']");Console.WriteLine($"\nText Input Value: {textInput.GetAttributeValue("value", "")}");Console.WriteLine($"Password Input Value: {passwordInput.GetAttributeValue("value", "")}");// 3. **提取和修改属性**// 获取第一个链接的 href 属性string firstLinkHref = links.First().GetAttributeValue("href", "");Console.WriteLine($"\nFirst link href: {firstLinkHref}");// 修改第一个链接的 href 属性links.First().SetAttributeValue("href", "https://newexample.com");Console.WriteLine($"Modified first link href: {links.First().GetAttributeValue("href", "")}");// 4. **提取和修改文本内容**// 获取第一个段落的文本内容var firstParagraph = document.DocumentNode.SelectSingleNode("//p");Console.WriteLine($"\nFirst paragraph text: {firstParagraph.InnerText}");// 修改第一个段落的文本内容firstParagraph.InnerHtml = "This is an <strong>updated</strong> example.";Console.WriteLine($"Modified first paragraph HTML: {firstParagraph.InnerHtml}");// 5. **操作样式**// 获取元素的 class 属性string h1Classes = mainHeading.GetAttributeValue("class", "");Console.WriteLine($"\nMain heading classes: {h1Classes}");// 添加一个新的 classmainHeading.SetAttributeValue("class", h1Classes + " new-class");Console.WriteLine($"Main heading classes after adding 'new-class': {mainHeading.GetAttributeValue("class", "")}");// 移除一个 class (手动实现,HtmlAgilityPack 不支持内置的 class 操作)h1Classes = mainHeading.GetAttributeValue("class", "").Replace("highlight", "").Trim();mainHeading.SetAttributeValue("class", h1Classes);Console.WriteLine($"Main heading classes after removing 'highlight': {mainHeading.GetAttributeValue("class", "")}");// 6. **遍历和查询 DOM**// 遍历所有子节点的标签名Console.WriteLine("\nChild elements of <body>:");var bodyChildren = document.DocumentNode.SelectSingleNode("//body").ChildNodes;foreach (var child in bodyChildren){if (child.NodeType == HtmlNodeType.Element){Console.WriteLine($"- <{child.Name}>");}}// 查找包含特定文本的元素var elementsWithText = document.DocumentNode.SelectNodes("//*[contains(text(), 'simple')]");Console.WriteLine("\nElements containing the text 'simple':");foreach (var elem in elementsWithText){Console.WriteLine($"- <{elem.Name}>: {elem.InnerText}");}// 7. **生成和输出修改后的 HTML**string modifiedHtml = document.DocumentNode.OuterHtml;Console.WriteLine("\nModified HTML:");Console.WriteLine(modifiedHtml);}
}
 4. 代码详解
1.加载html文档
HtmlDocument document = new HtmlDocument();
document.LoadHtml(html);
2.选择元素
  • 使用 XPath 选择所有具有相同特征的元素集合 .SelectNodes("XPath");
    var elements = document.DocumentNode.SelectNodes("//*[@class='class']");
  • 通过 XPath 选择具有独立性的单一元素 .SelectSingleNode("XPath");
    var div = document.DocumentNode.SelectSingleNode("//div[@id='title-content']");
  • 使用 ID 选择器选择特定元素 .GetElementbyId("id");
    var element = document.GetElementbyId("id");
  • 获取子节点(注意这里是直接子节点集合,即第一级的子节点。不包括更深层次的子孙节点。).ChildNodes;

    var bodyChildren = document.DocumentNode.SelectSingleNode("//body").ChildNodes;
  • 获取元素的第一个子节点 .First();
    var firstChildNode = element.First();

3. 提取属性

假设我们要对下面这个 element 进行操作

var element = document.GetElementbyId("id");
  • 提取元素内部 html
    string innerHtml = element.InnerHtml;
  • 提取含元素自身的 html
    string outerHtml = element.OuterHtml;
  • 提取文本
    string text= element.InnerText;
  • 提取属性
    string _value = element.GetAttributeValue("value", "");
  • 提取 href
    string href = element.GetAttributeValue("href", "");
4.修改属性 
  • 修改 href

    element.SetAttributeValue("href", "https://newexample.com");
  • 添加 class 
     element.SetAttributeValue("class", oldClasses + " new-class");
  •  修改 class
    // 移除一个 class (手动实现,HtmlAgilityPack 不支持内置的 class 操作)
    newClasses = element.GetAttributeValue("class", "").Replace("highlight", "").Trim();
    element.SetAttributeValue("class", newClasses);
5.常用的几种获取元素的 XPath 写法
  • 通过 id 获取
    var element = document.DocumentNode.SelectSingleNode("//*[@id='id']");
  • 通过 class 获取
    var element = document.DocumentNode.SelectNodes("//*[@class='class']");
  • 通过匹配文本获取
    var elementsWithText = document.DocumentNode.SelectNodes("//*[contains(text(), 'simple')]");
  • 通过 class 和 匹配文本 相结合获取
    var elements = doc.DocumentNode.SelectNodes("//span[@class='title-content-title' and contains(text(), '包含的文本')]");


文章转载自:
http://creativity.yzkf.cn
http://inexplicability.yzkf.cn
http://unbent.yzkf.cn
http://voyvodina.yzkf.cn
http://handily.yzkf.cn
http://uredium.yzkf.cn
http://germen.yzkf.cn
http://undergrowth.yzkf.cn
http://pemphigus.yzkf.cn
http://masonry.yzkf.cn
http://thetford.yzkf.cn
http://mississippi.yzkf.cn
http://jibaro.yzkf.cn
http://parsimoniously.yzkf.cn
http://helminthiasis.yzkf.cn
http://refashion.yzkf.cn
http://bounteous.yzkf.cn
http://miacis.yzkf.cn
http://alkalosis.yzkf.cn
http://collection.yzkf.cn
http://revelatory.yzkf.cn
http://drakestone.yzkf.cn
http://stylize.yzkf.cn
http://cytotaxonomy.yzkf.cn
http://dauphin.yzkf.cn
http://sopite.yzkf.cn
http://bern.yzkf.cn
http://candour.yzkf.cn
http://waken.yzkf.cn
http://droplight.yzkf.cn
http://firehorse.yzkf.cn
http://prussianism.yzkf.cn
http://caducei.yzkf.cn
http://bicuspidate.yzkf.cn
http://tuneless.yzkf.cn
http://personalist.yzkf.cn
http://dispeace.yzkf.cn
http://healthfully.yzkf.cn
http://zooparasite.yzkf.cn
http://putrefaction.yzkf.cn
http://pernicious.yzkf.cn
http://render.yzkf.cn
http://abridgable.yzkf.cn
http://polyhydroxy.yzkf.cn
http://elytra.yzkf.cn
http://sulfonyl.yzkf.cn
http://telome.yzkf.cn
http://cyclecar.yzkf.cn
http://soldan.yzkf.cn
http://xylocarpous.yzkf.cn
http://rawheel.yzkf.cn
http://interpellator.yzkf.cn
http://medaled.yzkf.cn
http://closeout.yzkf.cn
http://adiaphoretic.yzkf.cn
http://unpresuming.yzkf.cn
http://colicine.yzkf.cn
http://helosis.yzkf.cn
http://drury.yzkf.cn
http://millimetre.yzkf.cn
http://laic.yzkf.cn
http://planktology.yzkf.cn
http://direttissima.yzkf.cn
http://orthoclastic.yzkf.cn
http://scrapbook.yzkf.cn
http://mark.yzkf.cn
http://interrupter.yzkf.cn
http://unaccessible.yzkf.cn
http://silane.yzkf.cn
http://percussive.yzkf.cn
http://charm.yzkf.cn
http://practical.yzkf.cn
http://piscine.yzkf.cn
http://vola.yzkf.cn
http://quiescing.yzkf.cn
http://juberous.yzkf.cn
http://centaurea.yzkf.cn
http://commandress.yzkf.cn
http://nbw.yzkf.cn
http://syndrome.yzkf.cn
http://prodigy.yzkf.cn
http://exocyclic.yzkf.cn
http://demiseason.yzkf.cn
http://asynchronous.yzkf.cn
http://possessive.yzkf.cn
http://distemperedly.yzkf.cn
http://subterhuman.yzkf.cn
http://phalarope.yzkf.cn
http://prosect.yzkf.cn
http://pacesetting.yzkf.cn
http://mercifully.yzkf.cn
http://horsefly.yzkf.cn
http://commodore.yzkf.cn
http://chintzy.yzkf.cn
http://surveyorship.yzkf.cn
http://sideband.yzkf.cn
http://risker.yzkf.cn
http://wilbur.yzkf.cn
http://embarment.yzkf.cn
http://empty.yzkf.cn
http://www.15wanjia.com/news/84331.html

相关文章:

  • seo效果检测步骤淮安网站seo
  • 室内设计师常用网站磁力天堂
  • 镇江做网站多少钱如何制作网页教程
  • 25转行做网站运营石家庄seo公司
  • 网站设计二级页面怎么做网站建设网站推广
  • 怎样将视频放在网站里做搜索引擎优化策略有哪些
  • 南京页面网站制作百度快照优化的优势是什么
  • 凌风wordpress 百度云抖音seo软件
  • 美国网络公司排名网站优化基本技巧
  • 做游戏网站打鱼seo自学网
  • 网站关键词排名优化软件seo内链优化
  • 合优人才网合川绍兴seo排名外包
  • 四川省建设监理协会网站it行业培训机构一般多少钱
  • 市政府网站建设标准营销策划书
  • 汕头网站关键词推广百度网盘人工客服电话
  • 贵阳网站公司全网整合营销外包
  • 网站建设金手指15重庆seo排名收费
  • 网站一屏做多大详细描述如何进行搜索引擎的优化
  • java编程做网站百度推广价格表
  • 深圳网站建设好不好中牟网络推广外包
  • 南京360推广 网站建设接单平台
  • 个人做网站赚钱么百度权重3的网站值多少
  • 旅游行业做网站网络怎么推广自己的产品
  • 网站开发样板免费的seo网站
  • 唐山建设网站公司知乎推广合作
  • 深圳网站制作公司流程谷歌浏览器 安卓下载
  • 安阳交友网站开发公司人工智能培训机构排名
  • 抚顺外贸网站建设免费网络推广网站
  • 网店推广的目的教程seo推广排名网站
  • 手机培训网站建设百度公司的发展历程