使用Xpath从网页中获取数据
/// <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从网页中获取数据的更多相关文章
- Thymeleaf+SpringMVC,如何从模板中获取数据
Thymeleaf+SpringMVC,如何从模板中获取数据 在一个典型的SpringMVC应用中,带@Controller注解的类负责准备数据模型Map的数据和选择一个视图进行渲染.这个模型Map对 ...
- Web网页中动态数据区域的识别与抽取 Dynamical Data Regions Identification and Extraction in Web Pages
Web网页中动态数据区域的识别与抽取 Dynamical Data Regions Identification and Extraction in Web Pages Web网页中动态数据区域的识别 ...
- 网页中的数据的4个处理方式:CRUD(Creat, Retrive, Update, Delete)
网页中的数据的4个处理方式:CRUD(Creat, Retrive, Update, Delete) 2018-12-21, 后续完善
- hive从查询中获取数据插入到表或动态分区
Hive的insert语句能够从查询语句中获取数据,并同时将数据Load到目标表中.现在假定有一个已有数据的表staged_employees(雇员信息全量表),所属国家cnty和所属州st是该表的两 ...
- 哪种方式更适合在React中获取数据?
作者:Dmitri Pavlutin 译者:小维FE 原文:dmitripavlutin.com 国外文章,笔者采用意译的方式,以保证文章的可读性. 当执行像数据获取这样的I/O操作时,你必须发起获取 ...
- Django Form 实时从数据库中获取数据
修改 models.py 添加 class UserType(models.Model): caption = models.CharField(max_length=32) 执行命令,生成数据库 p ...
- SpringMVC从Request域中获取数据
SpringMVC从Request域中获取数据的三种方式 SpringMVC环境自行搭建, 约定存在如下目录和文件:/WEB-INF/pages/success.jsp 方式一:传入Model对象 前 ...
- SQL语句的使用,SELECT - 从数据库表中获取数据 UPDATE - 更新数据库表中的数据 DELETE - 从数据库表中删除数据 INSERT INTO - 向数据库表中插入数据
SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法. 但是 SQL 语言也包含用于更新. ...
- WebClient HttpWebRequest从网页中获取请求数据
WebClient HttpWebRequest //HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urlAddress) ...
随机推荐
- react native 遇到的坑
1.项目中新加入组件,应执行npm install命令 2.项目执行react-native run-android 报错,应进入android目录,执行gradlew.bat clean命令 3.L ...
- 利用 SQL Monitor 查看语句运行状态步骤
利用 SQL Monitor 查看语句运行状态步骤 1.确定语句被 SQL Monitor 监控 SQL> SELECT * FROM GV$SQL_MONITOR WHERE sql_id=' ...
- java实现抓取某公司官网新闻
这里先说一下,实习期的一个项目,当时并没有该合作公司的获取新闻的接口,但是项目又急着上线,所以总监就让我来做一个简单的抓取,现将主要的工具类NewsUtil.java贴出来供大家参考. NewsUti ...
- GitHub-撤销commit
以下方法适用于:撤销最后一次提交的情况 步骤如下: 步骤1.使用 ”git log“ 命令 找到 想要撤销的操作之前的一次提交的commit id : 比如:使用 git log命令如下结果 comm ...
- DATE_FORMAT() 函数
DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据DATE_FORMAT(now(),'%Y-%m-%d %T')
- Autofac实例生命周期
1.默认,每次请求都会返回一个实例 builder.RegisterType<X>().InstancePerDependency(); 2.Per Lifetime Scope:这个作用 ...
- Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改
A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...
- python常用功能总结
经常写python,但很多小的点都记不住,每用必查,总结下来,下次查自己的吧. 1.时间获取: import time print time.strftime("%Y-%m-%d %H:% ...
- 一个不错的JavaScript解析浏览器路径方法(转)
JavaScript中有时需要用到当前的请求路径等涉及到url的情况,正常情况下我们可以使用location对象来获取我们需要的信息,本文从另外一个途径来解决这个问题,而且更加巧妙 方法如下: fun ...
- 4.Maven概念模型,maven的生命周期,Maven坐标,依赖管理(依赖范围,依赖声明),仓库管理,私服概念
1 maven概念模型 2 maven的生命周期,项目构建过程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg== ...