/// <summary>
/// 从官方网站中抓取产品信息存放在本地数据库中
/// </summary>
/// <returns></returns>
public List<ProductMessage> GetlistProductMessage()
{ string html = GetProductsDescriptionsImage("http://www.grandcanyononepoint.com/products");
HtmlDocument document = new HtmlDocument();
document.LoadHtml(html);
HtmlNode rootNode = document.DocumentNode; /*//*[@class='list-product']为元素的XPath标记实例,
* 表示所有使用class="list-product"的节点
*/
HtmlNodeCollection rootNodeList = rootNode.SelectNodes("//*[@class='list-product']"); List<ProductMessage> products = new List<ProductMessage>();
foreach (HtmlNode node in rootNodeList)
{
ProductMessage db_product = new ProductMessage();
HtmlDocument docu = new HtmlDocument();
docu.LoadHtml(node.InnerHtml);
HtmlNode ro = docu.DocumentNode;
db_product.Code = Formsub(ro.SelectSingleNode("//*[@style='float:right;']").InnerText);
string Code = db_product.Code;
List<ProductMessage> Productlist = ProductMessage.GetProductList(Code,""); if (Productlist.Count>)
{
db_product.Name = Formsub(ro.SelectSingleNode("//*[@style='float:left;']").InnerText);
/*获取a节点中href标签的属性值*/
db_product.ID = GetProductID(ro.SelectSingleNode("a").Attributes["href"].Value);
string descmationhtml = GetProductsDescriptionsImage("http://www.grandcanyononepoint.com/products/view/" + db_product.ID + "");
HtmlDocument descmationDo = new HtmlDocument();
descmationDo.LoadHtml(descmationhtml);
HtmlNode descmationNode = descmationDo.DocumentNode;
db_product.Descmation = Formsub(descmationNode.SelectSingleNode("//*[@class='product-desc']").InnerHtml).Replace("'", ""); if (descmationNode.SelectSingleNode("//*[@class='details-tile']") != null)
{
db_product.DepartingFrom = Formsub(descmationNode.SelectSingleNode("//*[@class='details-tile']").InnerHtml.Replace("Departing From", ""));
}
if (descmationNode.SelectSingleNode("//*[@class='details-tile details-list']") != null)
{
db_product.ProductHighlights = Formsub(descmationNode.SelectSingleNode("//*[@class='details-tile details-list']").InnerHtml.Replace("Product Highlights", "")).Replace("'", "");
} #region
try
{
ProductMessage.UpdateWEBProductMessage(db_product.Descmation,db_product.DepartingFrom,db_product.ProductHighlights,db_product.Name,db_product.Code);
}
catch { }
#endregion #region
if (descmationNode.SelectSingleNode("//*[@class='product-equip']") != null)
{
HtmlDocument DesmationEquipment = new HtmlDocument();
DesmationEquipment.LoadHtml(descmationNode.SelectSingleNode("//*[@class='product-equip']").InnerHtml);
HtmlNode EquipmentNode = DesmationEquipment.DocumentNode;
HtmlNodeCollection EquipmentNodes = EquipmentNode.SelectNodes("div"); List<EquipmentModel> EquipmentString = new List<EquipmentModel>();
foreach (HtmlNode equipment in EquipmentNodes)
{
EquipmentModel Equipment_model = new EquipmentModel();
Equipment_model.Name = equipment.Attributes["title"].Value;
Equipment_model.ImageUrl = "/Papillon/EquipmentImage/" + equipment.Attributes["title"].Value + ".png"; try
{
ProductMessage.InsertProductEquipment(db_product.ID, Equipment_model.Name, Equipment_model.ImageUrl);
}
catch { }
EquipmentString.Add(Equipment_model);
}
db_product.Equipment = EquipmentString;
}
#endregion #region
if (descmationNode.SelectNodes("//*[@title='See full size image']") != null)
{
HtmlNodeCollection ImageNodes = descmationNode.SelectNodes("//*[@title='See full size image']");
List<ImageModel> ImageString = new List<ImageModel>();
foreach (HtmlNode imagenode in ImageNodes)
{
ImageModel image_model = new ImageModel(); HtmlDocument imageDo = new HtmlDocument();
imageDo.LoadHtml(imagenode.InnerHtml);
HtmlNode imgRo = imageDo.DocumentNode;
//原图片地址
string FromPath = "http://www.grandcanyononepoint.com" + imgRo.SelectSingleNode("img").Attributes["src"].Value; image_model.ImageUrl = FromPath;
try
{
ProductMessage.InsertProductImage(db_product.ID, image_model.ImageUrl);
}
catch { }
}
}
#endregion
products.Add(db_product);
}
}
return products;
}

Xpath是将html作为类似xml的格式进行获取的,主要通过节点的不同标示,获取不同内容,可以从网页中获取想要的数据,与网页爬虫不同。

使用Xpath从网页中获取数据的更多相关文章

  1. Thymeleaf+SpringMVC,如何从模板中获取数据

    Thymeleaf+SpringMVC,如何从模板中获取数据 在一个典型的SpringMVC应用中,带@Controller注解的类负责准备数据模型Map的数据和选择一个视图进行渲染.这个模型Map对 ...

  2. Web网页中动态数据区域的识别与抽取 Dynamical Data Regions Identification and Extraction in Web Pages

    Web网页中动态数据区域的识别与抽取 Dynamical Data Regions Identification and Extraction in Web Pages Web网页中动态数据区域的识别 ...

  3. 网页中的数据的4个处理方式:CRUD(Creat, Retrive, Update, Delete)

    网页中的数据的4个处理方式:CRUD(Creat, Retrive, Update, Delete) 2018-12-21, 后续完善

  4. hive从查询中获取数据插入到表或动态分区

    Hive的insert语句能够从查询语句中获取数据,并同时将数据Load到目标表中.现在假定有一个已有数据的表staged_employees(雇员信息全量表),所属国家cnty和所属州st是该表的两 ...

  5. 哪种方式更适合在React中获取数据?

    作者:Dmitri Pavlutin 译者:小维FE 原文:dmitripavlutin.com 国外文章,笔者采用意译的方式,以保证文章的可读性. 当执行像数据获取这样的I/O操作时,你必须发起获取 ...

  6. Django Form 实时从数据库中获取数据

    修改 models.py 添加 class UserType(models.Model): caption = models.CharField(max_length=32) 执行命令,生成数据库 p ...

  7. SpringMVC从Request域中获取数据

    SpringMVC从Request域中获取数据的三种方式 SpringMVC环境自行搭建, 约定存在如下目录和文件:/WEB-INF/pages/success.jsp 方式一:传入Model对象 前 ...

  8. SQL语句的使用,SELECT - 从数据库表中获取数据 UPDATE - 更新数据库表中的数据 DELETE - 从数据库表中删除数据 INSERT INTO - 向数据库表中插入数据

    SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法. 但是 SQL 语言也包含用于更新. ...

  9. WebClient HttpWebRequest从网页中获取请求数据

    WebClient HttpWebRequest //HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urlAddress) ...

随机推荐

  1. TypeScript学习笔记(四):函数

    这篇笔记我们来看看TypeScript中的函数. 函数类型 在JavaScript中存在两种定义函数的方法,如下: //命名函数 function add(x, y) { return x+y; } ...

  2. NET程序的破解--静态分析(Xenocode Fox 2006 Evaluation)

    NET程序已经红红火火的兴起,就象LINUX一样势不可挡的涌来.作为一名Cracker,你会选择躲避吗?嘿嘿,对俺而言,挑战更富有趣味. 破解好几个.NET的程序了,一直想写出来,只是时间问题,所以拖 ...

  3. c++回调函数

    dcc组件支持回调函数接口,当连接/断开连接对端时,调用传入的函数指针. A库和B库想做到不耦合,但是A库需要用到B库的某些函数,A库提供回调函数接口,在初始化的时候指定回调函数,降低耦合程度,每一个 ...

  4. opennebula 自定义安装目录

    /bin//mkinstalldirs /usr/local/lib /bin//mkinstalldirs /usr/local/include /bin//mkinstalldirs /usr/l ...

  5. Javascript 原型继承(续)—从函数到构造器的角色转换

    对于每一个声明的函数,里边都会带有一个prototype成员,prototype会指向一个对象,现在我们来聚焦prototype指向的这个对象,首先我们会认为,这个对象是一个该函数对应的一个实例对象, ...

  6. .net自动生成版本号

    在 AssemblyInfo.cs 文件中 修改 一下属性 [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFi ...

  7. URAL 2047 Maths 打表 递推

    MathsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...

  8. delphi TPopupMenu.Popup

      procedure TPopupMenu.Popup(X, Y: Integer);     这个点是相对桌面的而不是窗体的   GetCursorPos是鼠标的位置 鼠标动这个点就不一样   v ...

  9. 利用PHP生成二维码(转)

    导读:在二维码广泛应用化的今天,在web站点中自动生成对应的二维码是最基础的需求.文章介绍了使用PHP自动生成二维码的三种方式. get方法实现方式一: $urlToEncode="163. ...

  10. /proc/sysrq-trigger详解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://itnihao.blog.51cto.com/1741976/830374 htt ...