c#抓取网页数据(网络蜘蛛站点下的红色截图部分信息行业新闻信息解析 )
优采云 发布时间: 2021-10-09 01:07c#抓取网页数据(网络蜘蛛站点下的红色截图部分信息行业新闻信息解析
)
对于搜索引擎来说,爬取互联网上的所有网页几乎是不可能的。根据目前公布的数据,容量最大的搜索引擎只能抓取整个网页的40%左右。一方面是因为爬虫技术的瓶颈,无法遍历所有网页,还有很多网页无法从其他网页的链接中找到;
另一个原因是存储技术和处理技术的问题。如果每个页面的平均大小为 20K(包括图片),则 100 亿个网页的容量为 100×2000G 字节。就算能存储,下载还是有问题(按照A机每秒下载20K计算,340台机器连续下载一年才能下载所有网页)。同时,由于数据量大,在提供搜索时也会影响效率。因此,很多搜索引擎的网络蜘蛛只爬取那些重要的网页,爬取时评价重要性的主要依据是某个网页的链接深度。
【以下示例使用:以站点为例,抓取站点下信息行业新闻信息红色截图部分】
一、模拟请求,获取网页Html并下载html
1.1 首先通过浏览器查看所有访问网站的参数信息。
代码模拟请求时需要传递的所有参数信息都可以收录在下图红圈部分,但不一定需要和浏览器保持相同的参数。
1.2 代码实现,模拟请求
1 private string GetHtml(string Url) //:Url="http://www.cppi.cn";
2 {
3 string Html = string.Empty;
4 HttpWebRequest Request = HttpWebRequest.Create(Url) as HttpWebRequest;
5 Request.Timeout = 3000;
6 Request.ContentType = "text/html;charset=utf-8";
7 Encoding encode = Encoding.GetEncoding("GB2312");
8 using (HttpWebResponse response = Request.GetResponse() as HttpWebResponse)
9 {
10 if (response.StatusCode == HttpStatusCode.OK)
11 {
12 try
13 {
14 StreamReader stream = new StreamReader(response.GetResponseStream(), encode);
15 Html = stream.ReadToEnd();
16 }
17 catch (System.Exception ex)
18 {
19 throw ex;
20 }
21 }
22 }
23 return Html;
24 }
二、过滤数据
获取到Html内容后,我们需要对整个Html内容进行过滤,得到我们业务需要的数据。以下基于HtmlAgilityPack第三方组件,使用Path解析HTML。
HtmlAgilityPack是.net下的HTML解析库,见【】
2.1 通过浏览器,锁定要显示的信息对应的Xpath路径
通过浏览器[]访问站点,鼠标右键---勾选,定位到需要显示的节点,右键---复制-----复制Xpath
可以得到对应的Xpath路径:/html/body/div[8]/div[2]/ul/li
2.2 通过Xpath解析Html,使用HtmlAgilityPack获取信息;
1 public ApiResponseModel CrawlerIndustrynews()
2 {
3 string Url = ConfigurationManager.AppSettings.Get("NewsUrl");
4 List Industrynewslst = new List();
5 var Industrynews = new IndustrynewsResponse();
6 string html = GetHtml(Url); //:模拟请求获取到整个Html内容
7 if (!string.IsNullOrWhiteSpace(html))
8 {
9 HtmlDocument htmlDoc = new HtmlDocument();
10 htmlDoc.LoadHtml(html);
11 if (htmlDoc != null)
12 {
13 string maintitleXpath = "/html/body/div[8]/div[2]/h1[2]/a";
14 HtmlNode maintitleNode = htmlDoc.DocumentNode.SelectSingleNode(maintitleXpath);
15 if (maintitleNode != null)
16 {
17 Industrynews.NewsMainhref = Url + maintitleNode.Attributes["href"].Value;
18 Industrynews.NewsMainTitle = maintitleNode.InnerText;
19 }
20 string secondtitleXpath = "/html/body/div[8]/div[2]/p[2]";
21 HtmlNode SecondTitleNode = htmlDoc.DocumentNode.SelectSingleNode(secondtitleXpath);
22 if (SecondTitleNode != null)
23 {
24 Industrynews.SecondTitle = SecondTitleNode.InnerText;
25 }
26 string FocusXpath = "/html/body/div[8]/div[2]/ul/li";
27 HtmlNodeCollection htmlNode = htmlDoc.DocumentNode.SelectNodes(FocusXpath); //:该Xpath下有多个子节点,所以此处获取到所有的节点信息
28 if (htmlNode != null)
29 {
30 foreach (var node in htmlNode)//:循环子节点获取每个子节点对应信息
31 {
32 HtmlDocument docChild = new HtmlDocument();
33 docChild.LoadHtml(node.OuterHtml);
34 HtmlNode urlNode = docChild.DocumentNode.SelectSingleNode("li/a"); //获取a标签
35 if (docChild != null)
36 {
37 Industrynews industrynews = new Industrynews()
38 {
39 href = Url + urlNode.Attributes["href"].Value, //获取a标签对应的href属性
40 Text = urlNode.InnerText //:获取标签text显示文本
41 };
42 Industrynewslst.Add(industrynews);
43 }
44 }
45 Industrynews.IndustrynewsList = Industrynewslst;
46 }
47 return new ApiResponseModel { Code = (int)HttpStatusCode.OK, Message = "", Data = Industrynews };
48 }
49 else
50 {
51 return new ApiResponseModel { Code = (int)HttpStatusCode.BadRequest, Message = "ErrorLoadHtml", Data = null };
52 }
53 }
54 else
55 {
56 return new ApiResponseModel { Code = (int)HttpStatusCode.BadRequest, Message = "ErrorLoadHtml", Data = null };
57 }
58 }
三、以下截图可以截取需要的信息