A crawler for web ptt
以ptt_crawler.cfg中的index為起點開始抓, 抓到該版的最新一篇貼文 預設為[1, 1], 代表從board_name/index1.html中的第1篇文章開始抓, 抓到最後一筆
同時也會將下次要以哪篇文章為起點的index記錄下來 所以下次執行程式時就從該篇文章開始抓, 抓到最新的一篇貼文
文章會以以下兩種方式儲存:
- json格式 (posts/board_name/[M|G].[unsigned_integer].A.[HEX{3}].json)
- mysql (預設為開啟, 可使用參數
--no-database
來關閉)
- json
{
"board": 版名,
"url": [M|G].[unsigned_integer].A.[HEX{3}],
"author": 作者,
"title": 文章標題,
"datetime": 發文時間,
"ip": 發文者 IP,
"content": 文章內容,
"pushes": [
{
"status": 推/噓/→,
"userid": 推文者 ID,
"content": 推文內容,
"datetime": 推文時間
}
]
}
- mysql table schema請參考ptt.sql
如需使用mysql, 請建立my.cnf
, 預設路徑為~/.my.cnf
, 請自行更改
# in my.cnf
[client]
host = localhost
port = 3306
database = dbname
user = username
password = password
default-character-set = utf8
Python 3.4.3
$ python3 ptt_crawler.py (-b | --board) BOARD_NAME [--no-database]
$ python3 ptt_crawler.py gossiping # store both file and database
or
$ python3 ptt_crawler.py tainan --no-database # only store file
Inspired by PTTcrawler.