被標題吸引進來的不要罵我。
只是一個簡單的實現,隨手寫了來下載一部喜歡的小說的。示例中的小說只是示例,不是我的菜。
使用了jsoup。挺好用的一個工具。
有需要的話,參考下自己改吧。挺簡單的,是吧。
代碼如下:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package com.zhyea.doggie; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; public class Doggie { public static void main(String[] args){ try { File txtFile = new File( "D:/無限崩壞.txt" ); createTxtDoc(txtFile); addContent(txtFile); } catch (Exception e){ e.printStackTrace(); } } /** * 向小說文件中添加內容 * @param txtFile * 小說文件 * @throws IOException * @throws InterruptedException */ private static void addContent(File txtFile) throws IOException, InterruptedException{ appendTxt(txtFile, getBookInfo( "無限崩壞" , "啪啪啪狂魔" )); String url = "http://www.83kxs.com/View/12/12653/{pattern}.html" ; for ( int i= 5850686 ; i<= 5945501 ; i++){ try { String tmp = url.replace( "{pattern}" , i+ "" ); appendTxt(txtFile, getPageContent(tmp)); } catch (Exception e){ e.printStackTrace(); continue ; } } } /** * 設置書名和作者 * @param bookName * 書名 * @param author * 作者 * @return */ private static String getBookInfo(String bookName, String author){ return COMMON.replace( "{book}" , bookName).replace( "{author}" , author); } /** * 讀取頁面內容 * @param url * 訪問路徑 * @return * @throws IOException */ private static String getPageContent(String url) throws IOException{ String rtn = null ; Document doc = Jsoup.connect(url).get(); Elements content = doc.select( ".text p" ); Elements id="codetool">
以上就是小編為大家帶來的Java實現一個小說采集程序的簡單實例全部內容了,希望大家多多支持服務器之家~ 延伸 · 閱讀
精彩推薦
|