前言
作為一個新手,最近在學習c#,自己折騰弄了個簡單的小說爬蟲,實現了把小說內容爬下來寫入txt,還只能爬指定網站。
第一次搞爬蟲,涉及到了網絡協議,正則表達式,弄得手忙腳亂跑起來效率還差勁,慢慢改吧。下面話不多說了,來一起看看詳細的介紹吧。
爬的目標:http://www.166xs.com/xiaoshuo/83/83557/
一、先寫httpwebrequest把網站扒下來
這里有幾個坑,大概說下:
第一個就是記得弄個代理ip爬網站,第一次忘了弄代理然后ip就被封了。。。。。
第二個就是要判斷網頁是否壓縮,第一次沒弄結果各種轉碼gbk utf都是亂碼。后面解壓就好了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/// <summary> /// 抓取網頁并轉碼 /// </summary> /// <param name="url"></param> /// <param name="post_parament"></param> /// <returns></returns> public string httpget( string url, string post_parament) { string html; httpwebrequest web_request = (httpwebrequest)webrequest.create(url); web_request.timeout = 30000; web_request.method = "get" ; web_request.useragent = "mozilla/4.0" ; web_request.headers.add( "accept-encoding" , "gzip, deflate" ); //web_request.credentials = credentialcache.defaultcredentials; //設置代理屬性webproxy------------------------------------------------- webproxy proxy = new webproxy( "111.13.7.120" , 80); //在發起http請求前將proxy賦值給httpwebrequest的proxy屬性 web_request.proxy = proxy; httpwebresponse web_response = (httpwebresponse)web_request.getresponse(); if (web_response.contentencoding.tolower() == "gzip" ) // 如果使用了gzip則先解壓 { using (stream stream_receive = web_response.getresponsestream()) { using (var zip_stream = new gzipstream(stream_receive, compressionmode.decompress)) { using (streamreader stream_reader = new streamreader(zip_stream, encoding. default )) { html = stream_reader.readtoend(); } } } } else { using (stream stream_receive = web_response.getresponsestream()) { using (streamreader stream_reader = new streamreader(stream_receive, encoding. default )) { html = stream_reader.readtoend(); } } } return html; } |
二、下面就是用正則處理內容了,由于正則表達式不熟悉所以重復動作太多。
1.先獲取網頁內容
1
2
|
iwebhttprepository webhttprepository = new webhttprepository(); string html = webhttprepository.httpget(url_txt.text, "" ); |
2.獲取書名和文章列表
書名
文章列表
1
2
3
4
5
6
7
8
|
string novel_name = regex.match(html, @"(?<=<h1>)([\s\s]*?)(?=</h1>)" ).value; //獲取書名 regex regex_menu = new regex( @"(?is)(?<=<dl class=""book_list"">).+?(?=</dl>)" ); string result_menu = regex_menu.match(html).value; //獲取列表內容 regex regex_list = new regex( @"(?is)(?<=<dd>).+?(?=</dd>)" ); var result_list = regex_list.matches(result_menu); //獲取列表集合 |
3.因為章節列表前面有多余的<dd>,所以要剔除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
int i = 0; //計數 string menu_content = "" ; //所有章節 foreach (var x in result_list) { if (i < 4) { //前面五個都不是章節列表,所以剔除 } else { menu_content += x.tostring(); } i++; } |
4.然后獲取<a>的href和innerhtml,然后遍歷訪問獲得內容和章節名稱并處理,然后寫入txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
regex regex_href = new regex( @"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>" ); matchcollection result_match_list = regex_href.matches(menu_content); //獲取href鏈接和a標簽 innerhtml string novel_path = directory.getcurrentdirectory() + "\\novel\\" + novel_name + ".txt" ; //小說地址 file.create(novel_path).close(); streamwriter write_content = new streamwriter(novel_path); foreach (match result_single in result_match_list) { string url_text = result_single.groups[ "url" ].value; string content_text = result_single.groups[ "text" ].value; string content_html = webhttprepository.httpget(url_txt.text + url_text, "" ); //獲取內容頁 regex rege_content = new regex( @"(?is)(?<=<p class=""book_text"">).+?(?=</p>)" ); string result_content = rege_content.match(content_html).value; //獲取文章內容 regex regex_main = new regex( @"( )(.*)" ); string rsult_main = regex_main.match(result_content).value; //正文 string screen_content = rsult_main.replace( " " , "" ).replace( "<br />" , "\r\n" ); write_content.writeline(content_text + "\r\n" ); //寫入標題 write_content.writeline(screen_content); //寫入內容 } write_content.dispose(); write_content.close(); messagebox.show(novel_name+ ".txt 創建成功!" ); system.diagnostics.process.start(directory.getcurrentdirectory() + \\novel\\); |
三、小說寫入成功
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.cnblogs.com/xinyibufang/p/7615400.html