這不算爬蟲吧?!

小LiAn發表於2017-04-15

 ------------------------------------------------------------------------------------------------------------------

因程式需要,需要拿到一個粵語詞典(需要找到任一個漢字的粵語拼音),但是在網上找來找去都沒有找到現有的詞典。

走投無路下,只能對現有粵語詞典網站進行知識“掠奪”:),拿到一個對應表。

於是,碼了以下程式碼:

 1 using System;
 2 using System.Text;
 3 using System.Net;
 4 using System.IO;
 5 using System.Threading;
 6 
 7 namespace Yueyu_Dic_Crawler
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string[] array = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
14             {
15                 //建立檔案,準備輸入
16                 FileStream fs = new FileStream(@"C:\Users\Lian\Desktop\Dic\Dictionary.txt", FileMode.Create);
17                 StreamWriter sw = new StreamWriter(fs);
18 
19                 //由於此網站URL的特殊性,不用實現真正意義上的爬蟲就可以獲取資訊
20                 //只需要更改URL中間的4位就可以遍歷60000+漢字的資訊
21                 for (int apple=0;apple<16;apple++)
22                     for (int pear = 0; pear < 16; pear++)
23                         for (int orange = 0; orange < 16; orange++)
24                             for (int peach = 0; peach < 16; peach++)
25                             {
26                                 //沒有這個sleep,就要被網站伺服器的防護機制給弄炸了:(
27                                 Thread.Sleep(100);
28 
29                                 //從0000到FFFF:)
30                                 string url = "http://www.yueyv.cn/?keyword=%" + array[apple] + array[pear] + "%" + array[orange] + array[peach] + "&submit=%B2%E9+%D1%AF";
31 
32                                 //Request AND Response
33                                 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
34                                 request.Method = "GET";
35                                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
36 
37                                 //使用StreamReader讀取html原始碼
38                                 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
39 
40                                 //經觀察,第370行儲存漢字,第394行儲存粵語拼音
41                                 string hanzi_line;
42                                 for (int z = 0; z < 369; z++, reader.ReadLine()) ;
43                                 hanzi_line = reader.ReadLine();
44                                 string yuepin_line;
45                                 for (int z = 0; z < 23; z++, reader.ReadLine()) ;
46                                 yuepin_line = reader.ReadLine();
47 
48                                 //寫入檔案
49                                 sw.Write(hanzi_line + "\t" + yuepin_line + "\r\n");
50                                 Console.WriteLine(hanzi_line + "\t" + yuepin_line);
51 
52                                 /*@@@@@@!!!!!!@@@@@@!!!!!!@@@@@@!!!!!!@@@@@@*/
53 
54                                 //如果不關閉HttpWebResponse,在請求兩次後,就收不到迴音了= =
55                                 //應該算是C#的特點吧,很關鍵,花費了很長很長時間。
56                                 response.Close();
57                             }
58                 //清空緩衝區
59                 sw.Flush();
60                 //關閉流
61                 sw.Close();
62                 fs.Close();
63             }
64         }
65     }
66 }

其實,中間還有一些小細節,比如:

1、實際上只有一部分組合儲存著資訊,如8000-8FFF的組合中,其實只有8140-8FFE有資訊(感謝partner);

2、大約將0000-FFFF分成了10塊,分了10次才爬下來,因為即使sleep,伺服器的防護機制有時間也能把你攔住;

3、沒有使用正規表示式,就是用excel簡單處理了一下結果:)以後肯定要使用正規表示式:)

4、多音字,只收錄了它的第一次讀音:)

 

從昨天中午有這個想法,到今天晚上實現,感觸最深的有兩點:

一是,這個時代學習東西太方便了,知識的互動太便捷了!

二是,網際網路上儲存著多少知識和財富啊!!!!!!!!

 

過幾天把這個粵語詞典放網上:)應該不犯法吧。。。

 

                                                                                                                              小Lian

                                                                                                                          2017/4/15凌晨

相關文章