forked from onesup/p2p_book_sharing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawling_book.rb
268 lines (246 loc) · 11.3 KB
/
crawling_book.rb
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# coding : utf-8
#How to run: just type in console, 'CrawlingBook.start'
# 희귀사례 테스트용 url
# http://book.daum.net/detail/book.do?bookid=BOK00009259809YO
# 책 크기 정보 없음. 책 소개 없음.
# http://book.daum.net/detail/book.do?bookid=BOK00008912179BA
# isbn, 책 크기 정보 없음.
# http://book.daum.net/detail/book.do?bookid=KOR2005723000625
# isbn 10만 없음.
# http://book.daum.net/detail/book.do?bookid=KOR9788906035555
# 해당 책 정보는 제한되었습니다.
# http://book.daum.net/detail/book.do?bookid=BOK00003309287IN
require 'open-uri'
class CrawlingBook
attr_reader :categories
def self.put_item(item)
i = Book.new
i.url = item[:url]
i.title = item[:title]
i.foreign_title = item[:foreign_title]
i.first_category = item[:first_category]
i.second_category = item[:second_category]
i.authors = item[:author]
i.main_author = item[:author].first
i.publisher = item[:publisher]
i.published_at = item[:published_at]
i.language = item[:language]
i.size = item[:size]
i.last_page = item[:last_page]
i.isbn10 = item[:isbn10]
i.isbn13 = item[:isbn13]
i.price = item[:price]
i.reviewed = item[:reviewed]
i.description = item[:description]
i.index = item[:index]
i.series = item[:series]
i.save
end
def self.get_list(doc, nav_info = {})
list = Array.new
doc.xpath('//div[@id="page_body"]//ul[@class="listType"]//li/dl/dt/a').each do |book|
list << {:url => book["href"], :title => book.text}
end
nav_info[:page] = nav_info[:page] + 1
#매 10 페이지 다음 페이지마다 pageAction 이 1로 변경.
nav_info[:page_action] = (nav_info[:page] - 1) % 10 == 0 ? 1 : 0
nav_info[:min_value] = doc.xpath('//input[@id="minValue"]').first["value"].strip
nav_info[:max_value] = doc.xpath('//input[@id="maxValue"]').first["value"].strip
doc.xpath('//div[@id="page_body"]//ul[@class="listType"]//li/dl/dt/a').each do |book|
unless BookUrl.exists?(:url => book["href"])
b = BookUrl.new
b.url = book["href"]
b.title = book.text
b.save
else
puts "skip"
end
end
nav_info
end
def self.get_list_doc(nav_info = {})
nav_info[:page] ||= 1
nav_info[:page_action] ||= 0
nav_info[:max_value] ||= nil
nav_info[:min_value] ||= nil
nav_info[:category] ||= "KOR01"
nav_info[:last_page] ||= 3
url = "http://book.daum.net/category/book.do?cTab=06&disrate=0&sortType=1&recentType=0&viewType=01&saleStatus="
url = url + "&categoryID=" + nav_info[:category]
url = url + "&pageAction=" + nav_info[:page_action].to_s
unless nav_info[:max_value].nil?
url = url + "&maxValue=" + nav_info[:max_value] + "&minValue=" + nav_info[:min_value]
else
url = url + "&maxValue=&minValue="
end
url = url + "&pageNo=" + nav_info[:page].to_s
begin
doc = Nokogiri::HTML(open(url))
rescue
puts "connection error:::" + url
end
doc
end
def self.get_list_last_page(option = {})
option[:category] ||= "KOR01"
url = "http://book.daum.net/category/book.do?cTab=06&disrate=&sortType=1&recentType=0&viewType=01&saleStatus="
url += "&categoryID=" + option[:category]
doc = Nokogiri::HTML(open(url))
last_page = doc.xpath('//div[@id="pagingBook"]/span//em').last.text
last_page = last_page.split(',').join.to_i #3,019를 3019로 만들기.
# latest_date = doc.xpath('//span[@class="date"]').first
# latest_date = latest_date.text.to_date
last_page
end
def self.parse_item(url)
url = "http://book.daum.net" + url
item = Hash.new
begin
doc = Nokogiri::HTML(open(url))
if doc.xpath('//div[@id="error_content"]').empty? == true
authors = Array.new
reviewed = 0
doc.xpath('//dd[@id="author_info"]//a').each{|x| authors << x.text.strip}
doc.xpath('//a[@class="quote_num"]').each{|x| reviewed = reviewed + x.text.to_i}
item[:url] = url
item[:author] = authors
item[:reviewed] = reviewed
item[:title] = doc.xpath('//div[@id="page_body"]//h2[@class="title"]/span').first.text
item[:series] = doc.xpath('//div[@id="page_body"]//h2[@class="title"]/span').last.text
item[:language] = doc.xpath('//div[@id="page_body"]//div[@class="selectLay"]//strong')[0].text.strip
item[:first_category] = doc.xpath('//div[@id="page_body"]//div[@class="selectLay"]//strong')[1].text.strip
if doc.xpath('//div[@id="page_body"]//div[@class="selectLay"]//strong')[2].nil?
item[:second_category] = nil
else
item[:second_category] = doc.xpath('//div[@id="page_body"]//div[@class="selectLay"]//strong')[2].text.strip
end
item[:publisher] = doc.xpath('//dd[@id="publisher_info"]//a').text.strip
item[:published_at] = doc.xpath('//dd[@id="publisher_info"]').children[4].text.strip.to_date
if doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd').count == 5
item[:size] = doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd')[2].children[0].text.strip
if item[:last_page] = doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd')[2].count == 0
item[:last_page] = doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd')[2].text.strip
else
item[:last_page] = doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd')[2].children[2].text.strip
end
else
item[:size] = doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd')[3].children[0].text.strip
if doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd')[3].count == 0
item[:last_page] = doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd')[3].text.strip
else
item[:last_page] = doc.xpath('//div[@id="page_body"]/div[@class="topContWrap"]/div[@class="bookInfoArea"]/dl[@class="info"]//dd')[2].children[2].text.strip
end
end
if doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').children.count == 3 #한국 책
item[:isbn10] = doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').children[0].text.strip
item[:isbn13] = doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').children[2].text.strip
else #원제가 있는 번역 책
if doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').children[0].nil?
item[:foreign_title] = nil
else
item[:foreign_title] = doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').children[0].text.strip
end
if doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').count == 1
item[:isbn10] = doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').text.strip
item[:isbn13] = doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').text.strip
else
if doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').children[2].nil?
item[:isbn10] = nil
else
item[:isbn10] = doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').children[2].text.strip
end
if doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').empty?
item[:isbn13] = nil
else
item[:isbn13] = doc.xpath('//div[@id="etc_info"]//div[@class="textWrap"]').children[4].text.strip
end
end
end
item[:price] = doc.xpath('//dd[@id="price_info"]//strike').text
item[:price] = /-?\d+(,?\d*)*\.?\d*/.match(item[:price])[0].to_i if item[:price].kind_of?(Array)
if doc.xpath('//div[@id="page_body"]//div[@class="introd"]/dl/dd').empty?
item[:description] = nil
else
item[:description] = doc.xpath('//div[@id="page_body"]//div[@class="introd"]/dl/dd').children[2..7].text.strip
end
item[:index] = doc.xpath('//div[@id="page_body"]//div[@class="book_table"]//div').text.strip
end
rescue URI::InvalidURIError
item[:url] = url
end
item
end
def self.test
category = "KOR01"
last_page = CrawlingBook.get_list_last_page(:category => category)
nav_info = {:page => 1, :last_page => last_page, :category => category}
doc = CrawlingBook.get_list_doc(nav_info)
nav_info = CrawlingBook.get_list(doc, nav_info)
nav_info
end
def self.start
code = "KOR13"
CrawlingBook.url_start(code)
end
def self.url_start(code)
start_time = Time.now
last_page = CrawlingBook.get_list_last_page(:category => code)
nav_info = {:page => 1, :last_page => last_page + 1, :category => code}
last_page.times do |p|
puts p.to_s + "/" + last_page.to_s
doc = CrawlingBook.get_list_doc(nav_info)
nav_info = CrawlingBook.get_list(doc, nav_info)
end
end
def self.book_start(start_record = nil)
start_record ||= 500000 #350445
n_count = 0
BookUrl.find(:all, :limit => 100, :offset => n_count) do |i|
if i.id > start_record
puts i.id
unless Book.exists?(:url => "http://book.daum.net" + i.url)
puts i.id
book = CrawlingBook.parse_item(i.url)
CrawlingBook.put_item(book)
end
end
n_count += 100
end
end
def self.categories
[
{:name => "독일 소설", :code => "KOR0112"} #for test. that category has just 87pages.
# {:name => "소설", :code => "KOR01"}, v
# {:name => "시·에세이", :code => "KOR03"}, v
# {:name => "경제·경영", :code => "KOR13"}, v
# {:name => "자기계발", :code => "KOR15"}, v
#
# {:name => "유아", :code => "KOR41"}, v
# {:name => "아동", :code => "KOR42"}, v
# {:name => "중·고 학습", :code => "KOR25"}, v
# {:name => "어린이영어", :code => "KOR45"}, v
# {:name => "초등학습", :code => "KOR39"},
# {:name => "청소년", :code => "KOR38"},
# {:name => "취업·수험서", :code => "KOR31"},
# {:name => "가정·생활", :code => "KOR07"},
# {:name => "예술·대중문화", :code => "KOR23"},
# {:name => "취미·스포츠", :code => "KOR11"},
# {:name => "요리", :code => "KOR08"},
# {:name => "건강", :code => "KOR09"},
# {:name => "여행", :code => "KOR32"},
# {:name => "외국어", :code => "KOR27"},
# {:name => "사전", :code => "KOR37"},
# {:name => "잡지", :code => "KOR35"},
# {:name => "만화", :code => "KOR47"}, 2012. 11. 7
# {:name => "인문", :code => "KOR05"}, 2012. 11. 7
# {:name => "종교", :code => "KOR21"}, 2012. 11. 7
# {:name => "정치사회", :code => "KOR17"}, 2012. 11. 7
# {:name => "역사문화", :code => "KOR19"},
# {:name => "과학", :code => "KOR29"},
# {:name => "기술·공학", :code => "KOR26"},
# {:name => "컴퓨터·IT", :code => "KOR33"},
# {:name => "영어도서", :code => "ENG"},
# {:name => "일본도서", :code => "JAP"}
]
end
end