forked from luozhaohui/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportWishBooksFromDouban.py
More file actions
executable file
·421 lines (347 loc) · 12.6 KB
/
exportWishBooksFromDouban.py
File metadata and controls
executable file
·421 lines (347 loc) · 12.6 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env python
#! encoding=utf-8
# Author : [email protected]
# Blog : http://kesalin.github.io
# Date : 2016/07/13
# Description : 抓取豆瓣上指定标签的书籍并导出为 Markdown 文件,多线程版本.
# Version : 1.0.0.0
# Python Version: Python 2.7.3
# Python Queue : https://docs.python.org/2/library/queue.html
# Beautiful Soup: http://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/#
import os
import time
import timeit
import datetime
import re
import string
import urllib2
import math
from threading import Thread
from Queue import Queue
from bs4 import BeautifulSoup
# 获取 url 内容
gUseCookie = True
gHeaders = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
'Cookie': 'Put your cookie here'
}
def getHtml(url):
try:
if gUseCookie:
opener = urllib2.build_opener()
for k, v in gHeaders.items():
opener.addheaders.append((k, v))
response = opener.open(url)
data = response.read().decode('utf-8')
else:
request = urllib2.Request(url, None, gHeaders)
response = urllib2.urlopen(request)
data = response.read().decode('utf-8')
except urllib2.URLError as e:
if hasattr(e, "code"):
print("The server couldn't fulfill the request: " + url)
print("Error code: %s" % e.code)
elif hasattr(e, "reason"):
print("We failed to reach a server. Please check your url: " +
url + ", and read the Reason.")
print("Reason: %s" % e.reason)
return data
# 书籍信息类
class BookInfo:
def __init__(self, name, url, icon, num, people, comment):
self.name = name
self.url = url
self.icon = icon
self.ratingNum = num
self.ratingPeople = people
self.comment = comment
self.compositeRating = num
def __hash__(self):
return hash(self.url)
def __eq__(self, other):
if self.url == other.url:
return True
return False
def __sortByCompositeRating(self, other):
val = self.compositeRating - other.compositeRating
if val < 0:
return 1
elif val > 0:
return -1
else:
val = self.ratingPeople - other.ratingPeople
if val < 0:
return 1
elif val > 0:
return -1
else:
return 0
def __cmp__(self, other):
return self.__sortByCompositeRating(other)
# 导出为 Markdown 格式文件
def exportToMarkdown(filename, books, total):
path = "{0}.md".format(filename)
if(os.path.isfile(path)):
os.remove(path)
today = datetime.datetime.now()
todayStr = today.strftime('%Y-%m-%d %H:%M:%S %z')
file = open(path, 'a')
file.write('\n## 我想读的书)\n\n')
file.write('### 总计 {0:d} 本,更新时间:{1}\n'.format(total, todayStr))
i = 0
for book in books:
file.write('\n### No.{0:d} {1}\n'.format(i + 1, book.name))
file.write(' > **图书名称**: [{0}]({1}) \n'.format(book.name, book.url))
file.write(' > **豆瓣链接**: [{0}]({1}) \n'.format(book.url, book.url))
file.write(' > **豆瓣评分**: {0} \n'.format(book.ratingNum))
file.write(' > **评分人数**: {0} 人 \n'.format(book.ratingPeople))
file.write(' > **内容简介**: {0} \n'.format(book.comment))
i = i + 1
file.close()
# 解析图书信息
def parseItemInfo(minNum, maxNum, k, page, bookInfos):
soup = BeautifulSoup(page, 'html.parser')
# get book name
bookName = ''
bookImage = ''
tag = soup.find("a", 'nbg')
if tag:
bookName = tag['title'].strip().encode('utf-8')
bookImage = tag['href'].encode('utf-8')
#print(" > name: {0}, bookImage: {1}".format(bookName, bookImage))
# get description
description = ''
content = soup.find("div", "intro")
if content:
deses = content.find_all('p')
for des in deses:
if des and des.string:
intro = des.string.strip().encode('utf-8')
description = description + intro
#print(" > description: {0}".format(description))
# get book url
bookUrl = ''
content = soup.find("div", "indent")
if content:
tag = content.find("a")
if tag:
bookUrl = tag['href'].encode('utf-8')
bookUrl = bookUrl.replace('/new_offer', '/')
#print(" > url: {0}".format(bookUrl))
ratingNum = 0.0
ratingPeople = 0
content = soup.find("div", "rating_self clearfix")
if content:
tag = content.find("strong", "ll rating_num ")
if tag and tag.string:
ratingStr = tag.string.strip().encode('utf-8')
if len(ratingStr) > 0:
ratingNum = float(ratingStr)
content = soup.find("a", "rating_people")
if content:
tag = content.find('span')
if tag:
ratingStr = tag.string.strip().encode('utf-8')
if len(ratingStr) > 0:
ratingPeople = int(ratingStr)
#print(" > ratingNum: {0}, ratingPeople: {1}".format(ratingNum, ratingPeople))
# add book info to list
bookInfo = BookInfo(bookName, bookUrl, bookImage,
ratingNum, ratingPeople, description)
bookInfo.compositeRating = computeCompositeRating(
minNum, maxNum, k, ratingNum, ratingPeople)
bookInfos.append(bookInfo)
def parseItemUrlInfo(page, urls):
soup = BeautifulSoup(page, 'html.parser')
items = soup.find_all("li", "subject-item")
for item in items:
# print(item.prettify().encode('utf-8'))
# get item url
url = ''
content = item.find("div", "pic")
if content:
tag = content.find('a')
if tag:
url = tag['href'].encode('utf-8')
#print(" > url: {0}".format(url))
urls.append(url)
#=============================================================================
# 生产者-消费者模型
#=============================================================================
class Producer(Thread):
url = ''
def __init__(self, t_name, url, queue):
Thread.__init__(self, name=t_name)
self.url = url
self.queue = queue
def run(self):
page = getHtml(self.url)
if page:
# block util a free slot available
self.queue.put(page, True)
class Consumer(Thread):
running = True
books = []
queue = None
minNum = 5
maxNum = 5000
k = 0.25
def __init__(self, t_name, minNum, maxNum, k, queue, books):
Thread.__init__(self, name=t_name)
self.queue = queue
self.books = books
self.minNum = max(10, min(200, minNum))
self.maxNum = max(1000, min(maxNum, 20000))
self.k = max(0.01, min(1.0, k))
def stop(self):
self.running = False
def run(self):
while True:
if not self.running and self.queue.empty():
break
page = self.queue.get()
if page:
parseItemInfo(self.minNum, self.maxNum,
self.k, page, self.books)
self.queue.task_done()
class ParseItemUrlConsumer(Thread):
running = True
urls = []
def __init__(self, t_name, queue, urls):
Thread.__init__(self, name=t_name)
self.queue = queue
self.urls = urls
def stop(self):
self.running = False
def run(self):
while True:
if not self.running and self.queue.empty():
break
page = self.queue.get()
if page:
parseItemUrlInfo(page, self.urls)
self.queue.task_done()
def spider(username, minNum, maxNum, k):
print(' 抓取我想读的书 ...')
start = timeit.default_timer()
# all producers
queue = Queue(20)
bookInfos = []
producers = []
# get first page of doulist
wishUrl = "https://book.douban.com/people/{0}/wish".format(username)
page = getHtml(wishUrl)
if not page:
print(' > invalid url {0}'.format(wishUrl))
else:
# get url of other pages in doulist
soup = BeautifulSoup(page, 'html.parser')
content = soup.find("div", "paginator")
# print(content.prettify().encode('utf-8'))
nextPageStart = 100000
lastPageStart = 0
for child in content.children:
if child.name == 'a':
pattern = re.compile(r'(start=)([0-9]*)(.*)(&sort=)')
match = pattern.search(child['href'].encode('utf-8'))
if match:
index = int(match.group(2))
if nextPageStart > index:
nextPageStart = index
if lastPageStart < index:
lastPageStart = index
# process current page
queue.put(page)
urls = []
# create consumer
consumer = ParseItemUrlConsumer('ParseItemUrlConsumer', queue, urls)
consumer.start()
# create parge item url producers
# producers = []
for pageStart in range(nextPageStart, lastPageStart + nextPageStart, nextPageStart):
pageUrl = "https://book.douban.com/people/kesalin/wish?start={0:d}&sort=time&rating=all&filter=all&mode=grid".format(
pageStart)
producer = Producer('Producer_{0:d}'.format(
pageStart), pageUrl, queue)
producer.start()
producers.append(producer)
#print(" > process page : {0}".format(pageUrl))
time.sleep(0.1) # slow down a little
# wait for all producers
for producer in producers:
producer.join()
# wait for consumer
consumer.stop()
queue.put(None)
consumer.join()
urls = list(set(urls))
bookQueue = Queue(20)
producers.clear()
# create parse item consumer
consumer = Consumer('Consumer', minNum, maxNum,
k, bookQueue, bookInfos)
consumer.start()
print(" urls: ", len(urls))
# create parge item producers
for url in urls:
producer = Producer(url, url, bookQueue)
producer.start()
producers.append(producer)
#print(" > process item : {0}".format(url))
time.sleep(0.2) # slow down a little
# wait for all producers
for producer in producers:
producer.join()
# wait for consumer
consumer.stop()
queue.put(None)
consumer.join()
# summrise
total = len(bookInfos)
elapsed = timeit.default_timer() - start
print(" 获取 %d 本我想读的书,耗时 %.2f 秒" % (total, elapsed))
return bookInfos
def process(wishUrl, minNum, maxNum, k):
# spider
books = spider(wishUrl, minNum, maxNum, k)
if books:
books = list(set(books))
total = len(books)
print(" > 共获取 {0} 本我想读的书".format(total))
# sort
books = sorted(books)
# get top 100
#books = books[0:100]
# export to markdown
exportToMarkdown('我想读的书', books, total)
#=============================================================================
# 排序算法
#=============================================================================
def computeCompositeRating(minNum, maxNum, k, num, people):
people = max(1, min(maxNum, people))
if people <= minNum:
people = minNum / 3
peopleWeight = math.pow(people, k)
level4 = max(500, maxNum * 1 / 10)
level5 = max(1000, maxNum * 3 / 10)
if people < 50:
return (num * 40 + peopleWeight * 60) / 100.0
elif people < 100:
return (num * 50 + peopleWeight * 50) / 100.0
elif people < 200:
return (num * 60 + peopleWeight * 40) / 100.0
elif people < level4:
return (num * 70 + peopleWeight * 30) / 100.0
elif people < level5:
return (num * 80 + peopleWeight * 20) / 100.0
else:
return (num * 90 + peopleWeight * 10) / 100.0
#=============================================================================
# 程序入口:抓取指定标签的书籍
#=============================================================================
if __name__ == '__main__':
start = timeit.default_timer()
username = 'kesalin'
process(username, 30, 3000, 0.25)
elapsed = timeit.default_timer() - start
print("== 总耗时 %.2f 秒 ==" % (elapsed))