-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTT_Crawler.py
45 lines (36 loc) · 1.3 KB
/
PTT_Crawler.py
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
from PTTLibrary import PTT
import dill
ID = '帳號'
Password = '密碼'
PTTBot = PTT.Library()
PTTBot = PTT.Library(kickOtherLogin=False)
# 登入
ErrCode = PTTBot.login(ID, Password)
def PttCrawler(number,title):
globalDoc=[]
CrawPost=number
comment=[]
title2=[]
def PostHandler(Post):
temp=[]
for Push in Post.getPushList():
temp.append(Push.getContent())
globalDoc.append(Post.getContent())
comment.append(temp)
print(Post.getTitle())
title2.append(Post.getTitle())
ErrCode, NewestIndex = PTTBot.getNewestIndex(Board=title)
ErrCode, SuccessCount, DeleteCount = PTTBot.crawlBoard(title, PostHandler, StartIndex=NewestIndex - CrawPost + 1, EndIndex=NewestIndex)
while(len(globalDoc)<(number)):
CrawPost=number-len(globalDoc)
ErrCode, SuccessCount, DeleteCount = PTTBot.crawlBoard(title, PostHandler, StartIndex=NewestIndex - CrawPost + 1, EndIndex=NewestIndex)
#comment留言
#title2文章title
#globalDoc文章內容
with open (title+"_doc_Ptt",'wb') as file:
dill.dump(globalDoc,file)
with open (title+"_title_Ptt",'wb') as file:
dill.dump(title2,file)
with open (title+"_comment_Ptt",'wb') as file:
dill.dump(comment,file)
return(0)