百度地圖爬蟲(c#)

風靈使發表於2018-09-09

Program.cs

using System;
using System.Collections.Generic;


namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var queryDatas = new List<QueryData>() {
                new QueryData("1","南通",new string[]{ "學校","公園","火鍋"}),  //id只是標記,暫時無用
                new QueryData("2","上海",new string[]{ "學校","公園","火鍋"}),
                new QueryData("3","蘇州",new string[]{ "學校","公園","火鍋"})
            };
            WriteFileHelper.SearchData(queryDatas);
            Console.ReadLine();
        }
    }
}

QueryData.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace TestApp
{
    public class QueryData
    {
        public string id;
        public string[] pos;    //行政區劃區域檢索不支援多關鍵字檢索。
        public string region;
        public QueryData(string id, string region, string[] pos)
        {
            this.id = id;
            this.pos = pos;
            this.region = region;
        }
    }

}

WriteFileHelper.cs

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace TestApp
{
    class WriteFileHelper
    {

        public static async void SearchData(List<QueryData> queryDatas)
        {
            var ak = "Wc****************************5";
            DataTable dt = new DataTable();
            DataColumn c0 = new DataColumn("序號");
            DataColumn c1 = new DataColumn("地點名稱");
            DataColumn c2 = new DataColumn("地址");
            DataColumn c3 = new DataColumn("省");
            DataColumn c4 = new DataColumn("市");
            DataColumn c5 = new DataColumn("區");
            DataColumn c6 = new DataColumn("輸入的查詢關鍵字");
            DataColumn c7 = new DataColumn("lat");
            DataColumn c8 = new DataColumn("lng");

            dt.Columns.Add(c0);
            dt.Columns.Add(c1);
            dt.Columns.Add(c2);
            dt.Columns.Add(c3);
            dt.Columns.Add(c4);
            dt.Columns.Add(c5);
            dt.Columns.Add(c6);
            dt.Columns.Add(c7);
            dt.Columns.Add(c8);
            foreach (var queryData in queryDatas)
            {
                var id = queryData.id;
                var pos = queryData.pos;
                var region = queryData.region;
                foreach (var p in pos)
                {
                    dt = await WriteF(p, region, id, ak, dt);
                    System.Threading.Thread.Sleep(500);

                }
            }
            Console.WriteLine("開始寫入");
            SaveCSV(dt, Environment.CurrentDirectory + "\\data.csv");
            Console.WriteLine("寫入完成");
        }

        private async static Task<DataTable> WriteF(string pos, string region, string id, string ak, DataTable dt)
        {

            var url = string.Format("http://api.map.baidu.com/place/v2/search?query={0}&region={1}&output=json&page_size=20&ak={2}", pos, region, ak);
            Console.WriteLine(url);
            try
            {
                var jsonArray = await HttpGetRequest(url);
                foreach (var json in jsonArray)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = id;
                    dr[1] = json["name"];
                    dr[2] = json["address"];
                    dr[3] = json["province"];
                    dr[4] = json["city"];
                    dr[5] = json["area"];
                    dr[6] = pos;
                    if (json["location"] != null)
                    {
                        var lat = json["location"]["lat"].ToString();
                        var lng = json["location"]["lng"].ToString();

                        dr[7] = lat;
                        dr[8] = lng;

                    }
                    dt.Rows.Add(dr);

                }
            }
            catch (Exception e)
            {

            }
            return dt;
        }

        private static async Task<JArray> HttpGetRequest(string url)
        {
            HttpClient httpClient = new HttpClient();
            byte[] data = null;
            do
            {
                data = await httpClient.GetByteArrayAsync(url);
            } while (data == null);
            var jsonText = Encoding.UTF8.GetString(data);
            // Console.WriteLine(jsonText);
            JObject json = (JObject)JsonConvert.DeserializeObject(jsonText);
            JArray jsonArray = (JArray)JsonConvert.DeserializeObject(json["results"].ToString());
            return jsonArray;
        }

        /// <summary>
        /// 將DataTable中資料寫入到CSV檔案中
        /// </summary>
        /// <param name="dt">提供儲存資料的DataTable</param>
        /// <param name="fileName">CSV的檔案路徑</param>
        private static void SaveCSV(DataTable dt, string fullPath)
        {
            FileInfo fi = new FileInfo(fullPath);
            if (!fi.Directory.Exists)
            {
                fi.Directory.Create();
            }
            FileStream fs = new FileStream(fullPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            //StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
            string data = "";
            //寫出列名稱
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                data += dt.Columns[i].ColumnName.ToString();
                if (i < dt.Columns.Count - 1)
                {
                    data += ",";
                }
            }
            sw.WriteLine(data);
            //寫出各行資料
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                data = "";
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string str = dt.Rows[i][j].ToString();
                    str = str.Replace("\"", "\"\"");//替換英文冒號 英文冒號需要換成兩個冒號
                    if (str.Contains(",") || str.Contains("\"")
                        || str.Contains("\r") || str.Contains("\n")) //含逗號 冒號 換行符的需要放到引號中
                    {
                        str = string.Format("\"{0}\"", str);
                    }

                    data += str;
                    if (j < dt.Columns.Count - 1)
                    {
                        data += ",";
                    }
                }
                sw.WriteLine(data);
            }
            sw.Close();
            fs.Close();
            //DialogResult result = MessageBox.Show("CSV檔案儲存成功!");
            //if (result == DialogResult.OK)
            //{
            //    System.Diagnostics.Process.Start("explorer.exe", Common.PATH_LANG);
            //}
        }

    }
}

結果如圖:

這裡寫圖片描述

相關文章