/// <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. ActiveMQ JMS 在发邮件中的使用

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久 ...

  2. Codeforces Round #219 (Div. 1)(完全)

    戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...

  3. CloudStack 使用生态统计

    http://shapeblue.com/

  4. Bootstrap 列偏移\列嵌套\列排序

    1.列偏移 这个其实很简单就是通过一个样式类,通过.col-md-offset-*可以将列偏移到右侧.这些class通过使用*选择器将所有列增加了列的左侧margin.例如,.col-md-offse ...

  5. Java计算日期和时间差

    这篇文章将使用两个例子计算两个日期的时间差.1.使用Java SDK.2.使用Joda库.1.使用Java SDK 计算两个Date之间的时间差,基本思路为把Date转换为ms(微秒),然后计算两个微 ...

  6. 栈的应用2——超级计算器(中缀与后缀表达式)C语言

    输入中缀表达式输出结果(结果可以是小数,但输入必须是整数)  #include<stdio.h> #include<stdlib.h> //需要两个栈,一个储存结果,一个储存运 ...

  7. C++ 析构方法

    1.什么是析构方法? 析构方法与构造方法互补. 2.为什么设计析构方法? 构造方法创建一个对象,对象内部往往还会申请一些资源.设计析构方法的目的是 释放资源,同时销毁自身. 3.析构方法可以认为分为两 ...

  8. spring中的ResourceBundleMessageSource

    1 首先创建两个资源文件    messages_en_US.properties customer.name=Yong Mook Kim, age : {0}, URL : {1} messages ...

  9. android.content.res.Resources$NotFoundException:String resource ID #ffffffff

    无语,搞了半天,只能去插这个错误代号,结果就找到了这个结果. scoreTextView.setText(score+""); 这个一定要自己手动转换..不科学啊..关键是在ecl ...

  10. oc-10-函数与方法的区别

    .函数和对象方法的区别 以-开头的方法就是对象方法(即必须实例化对象才能使用的方法) 如: -(void)Run; 区别: ()语法区别,并且对象方法都以-号开头,函数直接以返回值开头 ()对象方法的 ...