在前几天看到一片公众号的文章是关于.NET玩爬虫。
所以今天小编索性来try一下,恰好小编最近在关注房价这一块的,索性就写了一个例子抓取房产信息的。
不善言辞的小编直接给出代码吧!相信读者也等不及了。你要是觉得有用就推荐一下或者评论一下吧!
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace CatchWeb
{
class Program
{
/// <summary>
///
/// 作者:haojieli
/// 时间:2017-02-21
/// 备注:HtmlAgilityPack例子
///
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
start();
}
public static void start()
{
Console.WriteLine("------------------------");
Console.WriteLine("---只可作为学习用途!");
Console.WriteLine("---作者:haojieli");
Console.WriteLine("---邮箱:2252487366@qq.com");
Console.WriteLine("------------------------");
Console.Write("是否开始抓取重庆链家的房源信息?输入Y或者N:");
String istrue = Console.ReadLine();
if (istrue == "Y" || istrue == "y")
{
String url = "http://cq.lianjia.com/xiaoqu/";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(GetHtml(url));
HtmlNodeCollection node = doc.DocumentNode.SelectNodes("html/body/div[4]/div[1]/ul/li");
StreamWriter sw = File.CreateText("log.txt");
sw.WriteLine("------------------------");
sw.WriteLine("---只可作为学习用途!");
sw.WriteLine("---作者:haojieli");
sw.WriteLine("---邮箱:2252487366@qq.com");
sw.WriteLine("------------------------");
foreach (HtmlNode li_nodes in node)
{
HtmlDocument titleDoc = new HtmlDocument();
titleDoc.LoadHtml(li_nodes.InnerHtml);
String str = titleDoc.DocumentNode.SelectNodes("//div[1]//div[1]")[0].InnerHtml;
HtmlDocument aDoc = new HtmlDocument();
aDoc.LoadHtml(str);
String xiaoquPrice = titleDoc.DocumentNode.SelectNodes("//div[2]//div[1]/span")[0].InnerHtml;
String xiaoquName = aDoc.DocumentNode.SelectNodes("//a")[0].InnerText;
String xiaoquUrl = aDoc.DocumentNode.SelectNodes("//a")[0].Attributes["href"].Value;
sw.WriteLine("小区名称:" + xiaoquName + " \r\n小区房源地址:" + xiaoquUrl + " \r\n小区平均价格:" + xiaoquPrice);
Console.WriteLine("小区名称:" + xiaoquName + " \r\n小区房源地址:" + xiaoquUrl + " \r\n小区平均价格:" + xiaoquPrice);
sw.WriteLine("----------------------------------------------------------");
Console.WriteLine("----------------------------------------------------------");
}
Console.WriteLine("---------------信息抓取完毕!");
Console.WriteLine("---------------请在log.txt下查看抓取信息!");
Console.WriteLine("---------------按任意键退出。");
sw.Close();
Console.ReadLine();
Environment.Exit(0);
}
else if (istrue == "N" || istrue == "n")
{
Environment.Exit(0);
}
else
{
Console.WriteLine("请输入对应指令!按任意键继续。");
Console.ReadLine();
start();
}
}
public static string GetHtml(string Url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
req.Method = "GET";
string str;
HttpWebResponse Stream = req.GetResponse() as HttpWebResponse;
if (Stream.CharacterSet.ToLower() == "gbk")
{
using (StreamReader reader = new StreamReader(Stream.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312")))
{
str = reader.ReadToEnd();
return str;
}
}
else
{
using (StreamReader reader = new StreamReader(Stream.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
{
str = reader.ReadToEnd();
return str;
}
}
}
}
}
其中getHtml()是在网上找的一个处理乱码的,具体是谁写的望地址了。在此还是感谢一下!
代码也就不怎么解释了,官方文档都有!主要是对于网页分析这一块的,使用了HtmlAgilityPack 。
HtmlAgilityPack的获取指定节点里面的内容是按照xpath来的 ,很简单的 自己看一下就可以了。如果想偷懒直接使用谷歌浏览器在console里面 选中你要抓取的代码内容,右键copy选项下面的Copy Xpath,具体看上面的代码 !我相信没人会看我敲的这段文字。就酱紫吧
运行效果: