c#爬蟲爬取京東的商品信息
前言
在一個(gè)小項(xiàng)目中,需要用到京東的所有商品ID,因此就用c#寫了個(gè)簡(jiǎn)單的爬蟲。
在解析HTML中沒(méi)有使用正則表達(dá)式,而是借助開源項(xiàng)目HtmlAgilityPack解析HTML。
下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧
一、下載網(wǎng)頁(yè)HTML
首先我們寫一個(gè)公共方法用來(lái)下載網(wǎng)頁(yè)的HTML。
在寫下載HTML方法之前,我們需要去查看京東網(wǎng)頁(yè)請(qǐng)求頭的相關(guān)信息,在發(fā)送請(qǐng)求時(shí)需要用到。
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();//讀取數(shù)據(jù)
sr.Close();
}
catch (Exception ex)
{
html = null;
}
}
}
}
catch (System.Net.WebException ex)
{
html = null;
}
catch (Exception ex)
{
html = null;
}
return html;
}
如上代碼所示,我們使用WebRequest來(lái)獲取網(wǎng)頁(yè)信息,在發(fā)送請(qǐng)求之前,需要先設(shè)置和京東頁(yè)面一樣的請(qǐng)求頭。
以上設(shè)置的信息比較簡(jiǎn)單,但能夠正常發(fā)送請(qǐng)求,我們也可以模擬瀏覽器設(shè)置cookie等等信息,
二、解析HTML
獲取所有商品的信息分為兩個(gè)步驟
(1)根據(jù)商品分類頁(yè)面獲取所有商品分類的URL
(2)根據(jù)商品分類URL獲取每個(gè)商品
1、獲取商品分類
try
{
string html = HttpHelper.DownloadUrl(@"http://www.jd.com/allSort.aspx");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
string goodClass= @"http://*[@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來(lái)解析HTML信息,這是.NET的開源項(xiàng)目,開源在nuget包中下載。
(1)下載http://www.jd.com/allSort.aspx的html頁(yè),然后加載到HtmlDocument
(2)選擇節(jié)點(diǎn),獲取每個(gè)大類的節(jié)點(diǎn)集合
(3)根據(jù)每個(gè)大類的節(jié)點(diǎn),獲取每個(gè)小類的節(jié)點(diǎn)信息,然后獲取到分類地址
節(jié)點(diǎn)中也包含了其它很多信息,可以根據(jù)自己的需求去獲取對(duì)應(yīng)的信息
2、獲取具體商品信息
(1)首先根據(jù)商品分類加載分類信息,獲取到當(dāng)前分類每個(gè)頁(yè)面的鏈接
下載HTML之后,選擇節(jié)點(diǎn),可以將HTML格式化之后查看每個(gè)頁(yè)面的url地址和拼接規(guī)則,然后借助HtmlAgilityPack
來(lái)篩選需要的節(jié)點(diǎn),將每個(gè)頁(yè)面的url分離出來(lái)
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(@"http://*[@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)根據(jù)每個(gè)頁(yè)面的鏈接,獲取當(dāng)前頁(yè)面的商品信息
下載每個(gè)頁(yè)面的所有商品信息,需要獲取的商品信息在頁(yè)面中都能找到
首先我們獲取到每個(gè)商品的節(jié)點(diǎn)集合,獲取到一個(gè)商品的節(jié)點(diǎn)信息之后,分析html數(shù)據(jù),
找到我們需要的商品的信息所在的位置,然后將需要的信息分離出來(lái)。
下面的代碼中我獲取到的商品的id和title還有價(jià)格。
List<ProductInfo> productInfoList = new List<ProductInfo>();
try
{
string html = HttpHelper.DownloadUrl(url);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNodeCollection productNodeList = doc.DocumentNode.SelectNodes("http://*[@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("http://*[@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("http://*[@class='p-name']/a/em");
if (titleNode == null)
{
continue;
}
productInfo.Title = titleNode.InnerText;
HtmlNode priceNode = docChild.DocumentNode.SelectSingleNode("http://*[@class='p-price']/strong/i");
if (priceNode == null)
{
continue;
}
else
{
}
productInfoList.Add(productInfo);
}
//批量獲取價(jià)格
GetGoodsPrice(productInfoList);
}
catch (Exception ex)
{
}
return productInfoList;
商品的圖片地址和價(jià)格信息的獲取需要仔細(xì)分析html中的數(shù)據(jù),然后找到規(guī)律,比如價(jià)格在每個(gè)節(jié)點(diǎn)中就不能單獨(dú)獲取。
以下為批量獲取價(jià)格的代碼:
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;
以上就是一個(gè)簡(jiǎn)單的爬取京東商品信息的爬蟲,也可以根據(jù)自己的需求去解析更多的數(shù)據(jù)出來(lái)。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
C#基于winform實(shí)現(xiàn)音樂(lè)播放器
這篇文章主要為大家詳細(xì)介紹了C#基于winform實(shí)現(xiàn)音樂(lè)播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
c#測(cè)試本機(jī)sql運(yùn)算速度的代碼示例分享
本文代碼目的很簡(jiǎn)單,就是使用c#測(cè)試一下本機(jī)sql運(yùn)算的速度,使用循環(huán)往數(shù)據(jù)里大量插入數(shù)據(jù),計(jì)算所用時(shí)間,大家參考使用吧2014-01-01
winform 實(shí)現(xiàn)選擇文件和選擇文件夾對(duì)話框的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇winform 實(shí)現(xiàn)選擇文件和選擇文件夾對(duì)話框的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
C# WinForm狀態(tài)欄實(shí)時(shí)顯示當(dāng)前時(shí)間(窗體狀態(tài)欄StatusStrip示例)
這篇文章主要介紹了C# WinForm狀態(tài)欄實(shí)時(shí)顯示當(dāng)前時(shí)間(窗體狀態(tài)欄StatusStrip示例),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
C#實(shí)現(xiàn)簡(jiǎn)單的RSA非對(duì)稱加密算法示例
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的RSA非對(duì)稱加密算法,結(jié)合實(shí)例形式分析了C#實(shí)現(xiàn)RSA加密的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
c#使用簡(jiǎn)單工廠模式實(shí)現(xiàn)生成html文件的封裝類分享
這篇文章主要介紹了運(yùn)用了簡(jiǎn)單工廠模式實(shí)現(xiàn)頁(yè)面靜態(tài)化封裝類,思路比較簡(jiǎn)單,大家可根據(jù)自己的思路再擴(kuò)展此類2014-01-01
C#中使用FilleStream實(shí)現(xiàn)視頻文件的復(fù)制功能
這篇文章主要介紹了C#中使用FilleStream實(shí)現(xiàn)視頻文件的復(fù)制功能,本文通過(guò)實(shí)例代碼給大家介紹的非常想詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

