c#爬虫爬取京东的商品信息
作者:alone_alone 发布时间:2022-12-03 14:38:11
前言
在一个小项目中,需要用到京东的所有商品ID,因此就用c#写了个简单的爬虫。
在解析HTML中没有使用正则表达式,而是借助开源项目HtmlAgilityPack解析HTML。
下面话不多说了,来一起看看详细的介绍吧
一、下载网页HTML
首先我们写一个公共方法用来下载网页的HTML。
在写下载HTML方法之前,我们需要去查看京东网页请求头的相关信息,在发送请求时需要用到。
public static string DownloadHtml(string url, Encoding encode)
{
string html = string.Empty;
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Timeout = 30 * 1000;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
request.ContentType = "text/html; charset=utf-8";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.OK)
{
try
{
StreamReader sr = new StreamReader(response.GetResponseStream(), encode);
html = sr.ReadToEnd();//读取数据
sr.Close();
}
catch (Exception ex)
{
html = null;
}
}
}
}
catch (System.Net.WebException ex)
{
html = null;
}
catch (Exception ex)
{
html = null;
}
return html;
}
如上代码所示,我们使用WebRequest来获取网页信息,在发送请求之前,需要先设置和京东页面一样的请求头。
以上设置的信息比较简单,但能够正常发送请求,我们也可以模拟浏览器设置cookie等等信息,
二、解析HTML
获取所有商品的信息分为两个步骤
(1)根据商品分类页面获取所有商品分类的URL
(2)根据商品分类URL获取每个商品
1、获取商品分类
try
{
string html = HttpHelper.DownloadUrl(@"http://www.jd.com/allSort.aspx");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
string goodClass= @"//*[@class='items']/dl/dd";
HtmlNodeCollection noneNodeList = doc.DocumentNode.SelectNodes(goodClass);
foreach (var node in noneNodeList)
{
HtmlDocument docChild = new HtmlDocument();
docChild.LoadHtml(node.OuterHtml);
string urlPath = "/dd/a";
HtmlNodeCollection list = docChild.DocumentNode.SelectNodes(urlPath);
foreach (var l in list)
{
HtmlDocument docChild1 = new HtmlDocument();
docChild1.LoadHtml(l.OuterHtml);
var sortUrl = l.Attributes["href"].Value;
if (!string.IsNullOrWhiteSpace(sortUrl) && sortUrl.Contains("cat="))
{
InsertSort("https:" + sortUrl);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
上面的代码中使用到了HtmlAgilityPack来解析HTML信息,这是.NET的开源项目,开源在nuget包中下载。
(1)下载http://www.jd.com/allSort.aspx的html页,然后加载到HtmlDocument
(2)选择节点,获取每个大类的节点集合
(3)根据每个大类的节点,获取每个小类的节点信息,然后获取到分类地址
节点中也包含了其它很多信息,可以根据自己的需求去获取对应的信息
2、获取具体商品信息
(1)首先根据商品分类加载分类信息,获取到当前分类每个页面的链接
下载HTML之后,选择节点,可以将HTML格式化之后查看每个页面的url地址和拼接规则,然后借助HtmlAgilityPack
来筛选需要的节点,将每个页面的url分离出来
try
{
string html = HttpHelper.DownloadUrl(@"https://list.jd.com/list.html?cat=1620,11158,11964");
HtmlDocument productDoc = new HtmlDocument();
productDoc.LoadHtml(html);
HtmlNode pageNode = productDoc.DocumentNode.SelectSingleNode(@"//*[@id='J_topPage']/span/i");
if (pageNode != null)
{
int pageNum = Convert.ToInt32(pageNode.InnerText);
for (int i = 1; i < pageNum + 1; i++)
{
string pageUrl = string.Format("{0}&page={1}", category.Url, i).Replace("&page=1&", string.Format("&page={0}&", i));
try
{
List<ProductInfo> proDuctInfo = GetPageProduct(pageUrl);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
(2)根据每个页面的链接,获取当前页面的商品信息
下载每个页面的所有商品信息,需要获取的商品信息在页面中都能找到
首先我们获取到每个商品的节点集合,获取到一个商品的节点信息之后,分析html数据,
找到我们需要的商品的信息所在的位置,然后将需要的信息分离出来。
下面的代码中我获取到的商品的id和title还有价格。
List<ProductInfo> productInfoList = new List<ProductInfo>();
try
{
string html = HttpHelper.DownloadUrl(url);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNodeCollection productNodeList = doc.DocumentNode.SelectNodes("//*[@id='plist']/ul/li");
if (productNodeList == null || productNodeList.Count == 0)
{
return productInfoList;
}
foreach (var node in productNodeList)
{
HtmlDocument docChild = new HtmlDocument();
docChild.LoadHtml(node.OuterHtml);
ProductInfo productInfo = new ProductInfo()
{
CategoryId = category.Id
};
HtmlNode urlNode = docChild.DocumentNode.SelectSingleNode("//*[@class='p-name']/a");
if (urlNode == null)
{
continue;
}
string newUrl= urlNode.Attributes["href"].Value;
newUrl = !newUrl.StartsWith("http:")?"http:" + newUrl: newUrl;
string sId = Path.GetFileName(newUrl).Replace(".html", "");
productInfo.ProductId = long.Parse(sId);
HtmlNode titleNode = docChild.DocumentNode.SelectSingleNode("//*[@class='p-name']/a/em");
if (titleNode == null)
{
continue;
}
productInfo.Title = titleNode.InnerText;
HtmlNode priceNode = docChild.DocumentNode.SelectSingleNode("//*[@class='p-price']/strong/i");
if (priceNode == null)
{
continue;
}
else
{
}
productInfoList.Add(productInfo);
}
//批量获取价格
GetGoodsPrice(productInfoList);
}
catch (Exception ex)
{
}
return productInfoList;
商品的图片地址和价格信息的获取需要仔细分析html中的数据,然后找到规律,比如价格在每个节点中就不能单独获取。
以下为批量获取价格的代码:
try
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("http://p.3.cn/prices/mgets?callback=jQuery1069298&type=1&area=1_72_4137_0&skuIds={0}&pdbp=0&pdtk=&pdpin=&pduid=1945966343&_=1469022843655", string.Join("%2C", productInfoList.Select(c => string.Format("J_{0}", c.ProductId))));
string html = HttpHelper.DownloadUrl(sb.ToString());
if (string.IsNullOrWhiteSpace(html))
{
return productInfoList;
}
html = html.Substring(html.IndexOf("(") + 1);
html = html.Substring(0, html.LastIndexOf(")"));
List<CommodityPrice> priceList = JsonConvert.DeserializeObject<List<CommodityPrice>>(html);
productInfoList.ForEach(c => c.Price = priceList.FirstOrDefault(p => p.id.Equals(string.Format("J_{0}", c.ProductId))).p);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return productInfoList;
来源:http://www.cnblogs.com/xxue/p/9977801.html


猜你喜欢
- 前言最近在看王清培前辈的.NET框架设计时,当中有提到扩展方法 .开头的一句话是:扩展方法是让我们在不改变类原有代码的情况下动态地添加方法的
- 前言 之前unity5.x在代码中写了debug.log..等等,打
- 对于开发游戏项目的同胞来说,Timer 这个东西肯定不会陌生,今天对以前自己经常使用的定时进行了一番小小的总结!没有写具体实现的原理,只是列
- 指示器时间轴在外卖、购物类的APP里会经常用到,效果大概就像下面这样,看了网上很多文章,大都是自己绘制,太麻烦,其实通过ListView就可
- 本文实例讲述了Android开发之滑动数值选择器NumberPicker用法。分享给大家供大家参考,具体如下:简介:NumberPicker
- 本文实例讲述了Android使用ToggleButton实现开关效果的方法。分享给大家供大家参考,具体如下:activity_main.xm
- 委托:顾名思义,让别人帮你办件事。委托是C#实现回调函数的一种机制。可能有人会问了,回调函数是个啥???举个例子:我现在是一家公司的老板,公
- 桶排序桶排序是计数排序的升级,计数排序可以看成每个桶只存储相同元素,而桶排序每个桶存储一定范围的元素,通过函数的某种映射关系,将待排序数组中
- springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-start
- 1.之前在使用AutoMapper 框架感觉用着比较不够灵活,而且主要通过表达式树Api 实现对象映射 ,写着比较讨厌,当出现复杂类型和嵌套
- 1、对称二叉树【OJ链接】分为以下几种情况:二叉树为空,是对称二叉树二叉树不为空,其左子树或者右子树为空,不是对称二叉树二叉树不为空,左右子
- 之前花了几天去研究怎么使用netty做一个网关服务器,虽然最后还是没能用上我做的网关,但是呢netty是会用了,总结一下netty和spri
- 上次面试中遇到的一个问题,问到System.out.println()中的out是不是内部类,当时就给问蒙了,直观感觉out应该是Syste
- 这里说是框架,说的大了点,其实没有那么复杂,只是一个容易扩展的基类而已。不过至少算是框架类的代码。package arui; i
- /** * 日期工具类 * 默认使用 "yyyy-MM-dd HH:mm:ss" 格式化日期&nbs
- Mybatis注解开发单表操作MyBatis的常用注解Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper映射文件了。我
- 本文实例为大家分享了Unity shader实现消融效果的具体代码,供大家参考,具体内容如下效果图:shader代码:// Upgrade
- Common.cs: using System; using System.Collections.Generic; using Syste
- 本文实例讲述了C#直线的最小二乘法线性回归运算方法。分享给大家供大家参考。具体如下:1.Point结构在编写C#窗体应用程序时,因为引用了S
- 简介在上一篇文章中,我们列举了flutter中的所有layout类,并且详细介绍了两个非常常用的layout:Row和Column。掌握了上