美文网首页爬虫专题.netC#
C#爬豆瓣音乐-DC学院学习笔记

C#爬豆瓣音乐-DC学院学习笔记

作者: 宁冬青 | 来源:发表于2018-02-08 10:00 被阅读20次

步骤:

1.下载豆瓣音乐页面

2.分析目标信息(tag信息)所在位置,使用正则提取出目标元素

3.标准化表格形式的数据并导出到csv文件

具体步骤:

1.新建项目;

2.通过分析步骤,编写实现代码 实现代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//添加应用,添加表格保存数据,添加正则引用,添加IO写入写出
using System.Data;
using System.Text.RegularExpressions;
using System.IO;

namespace class1
{
class Program
{
static void Main(string[] args)
{
//html下载
byte[] buffer = null;
System.Net.WebClient webclient = new System.Net.WebClient();
buffer = webclient.DownloadData("https://music.douban.com/tag/%E6%91%87%E6%BB%9A?start=0&type=T");
//utf-8,gb2312,gbk,utf-16...
string html = System.Text.Encoding.GetEncoding("utf-8").GetString(buffer);

        //html分析
        //通过正则表达式获取需要的数据导出到csv
        MatchCollection matchs = new Regex("<p class=\"pl\">([\\s\\S]*?)</p>").Matches(html);
        int count = matchs.Count;
        string[] input = new string[count];
        for (int i = 0; i < count; i++)
        {
            input[i] = matchs[i].Result("$1");
        }

        DataTable dt = new DataTable();
        DataColumnCollection col = dt.Columns;
        col.Add("tag信息",typeof(System.String));
        for (int i = 0; i < input.Length; i++)
        {
            DataRow row = dt.NewRow();
            row["tag信息"] = input[i];
            dt.Rows.Add(row);
        }
        //html导出csv
        //标准化表格形式的数据导出到csv文件

        string filename = "豆瓣音乐测试导出.csv";
        //创建一个文件流
        FileStream fs = new FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
        //创建一个文件写入对象
        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
        //开始导出数据
        string data = "";
        //写出列名称
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            data += dt.Columns[i].ColumnName.ToString();
            //之前的需要逗号分隔,csv的默认分割符号,最后一位不要所以是count-1
            if (i < dt.Columns.Count - 1)
            {
                data += ",";
            }
        }
        //写入一行,也就是csv的一行数据
        sw.WriteLine(data);
        //写出各行数据
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            data = "";
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                //每一行数据做一下处理,去除换行的所有的分隔符也就是逗号
                data += dt.Rows[i][j].ToString();
                //这里和上面一样的原理
                if (j < dt.Columns.Count - 1)
                {
                    data += ",";
                }
            }
            //这里和上面的一样,写入一行
            sw.WriteLine(data);
        }
        //写完之后关闭文件流和写入文件的对象
        sw.Close();
        fs.Close();
        System.Console.WriteLine("完成一页tag信息采集,导出完成");
    }
}

}

3.在项目路径(*\项目名\项目名\bin\Debug)下找到目标信息文件就可以了

(自我碎碎念:加一个学习地址方便下次直接跳转:https://www.dcxueyuan.com//classDetail/classIntroduce/22/page.html?slxydc=fa1a99

相关文章

网友评论

  • 宁冬青:加一个邀请(优惠)码吧,如果有需要的话可以用:AFAJ9I

本文标题:C#爬豆瓣音乐-DC学院学习笔记

本文链接:https://www.haomeiwen.com/subject/tlgzzxtx.html