前言
在一个小项目中,需要用到京东的所有商品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 = GetPageProduct(pageUrl);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
(2)根据每个页面的链接,获取当前页面的商品信息
下载每个页面的所有商品信息,需要获取的商品信息在页面中都能找到
首先我们获取到每个商品的节点集合,获取到一个商品的节点信息之后,分析html数据,
找到我们需要的商品的信息所在的位置,然后将需要的信息分离出来。
下面的代码中我获取到的商品的id和title还有价格。
ListproductInfoList = new List (); 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 priceList = JsonConvert.DeserializeObject>(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;
以上就是一个简单的爬取京东商品信息的爬虫,也可以根据自己的需求去解析更多的数据出来。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。



