[心得] 取得聯合報歷史頭版
聯合報有個網站可以看到各年的頭版,分享抓取的程式,參考參考..
2014-01-06 改為多執行緒
------------------------------------------------------------
#!/usr/bin/env python
#!coding=utf-8
import os
import sys
import time
import pycurl
import datetime
import threading
from PIL import Image
from StringIO import StringIO
class GetPic(threading.Thread):
def __init__(self, string, id):
threading.Thread.__init__(self)
self.setName(str(id))
self.lastLen = 1
self.lock = threading.Lock()
def run(self):
while 1:
#取得鎖定
if self.lock.acquire():
#取出年份
if len(PP):
#取得任務
self.y = PP.pop(0)
self.lock.release()
else:
#已經沒有任務了,退出線程
self.lock.release()
print time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime())+" Thread id:"+self.getName()+" 結束."
#跳出迴圈線程結束
break
else:
continue
#確認年份目錄
self.yString = str(self.y)
self.path = os.path.normpath(path+"/"+self.yString)
if not os.path.isdir(self.path):
os.makedirs(self.path)
print "目錄 "+self.path+" 不存在,產生新目錄."
print time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime())+" Thread id:"+self.getName()\
+" 開始擷取 "+self.yString+"年."
#取出月份
for self.m in xrange(1, 13):
#1951年9月16日前資料不存在所以跳過,寫法不是很好,將就一下
if self.y == 1951 and self.m < 9:
continue
#未來資料不存在所以跳過
if self.y == now.tm_year and self.m > now.tm_mon:
continue
#幫月份小於十的月份前加上0
if self.m < 10:
self.mString = "0"+str(self.m)
else:
self.mString = str(self.m)
#取出日期
for self.d in xrange(1, 32):
#未來資料不存在所以跳過
if self.y == now.tm_year and self.m == now.tm_mon \
and self.d > now.tm_mday:
continue
self.dString = str(self.d)
#幫日期小於十的日期前加上0
if self.d < 10:
self.dString = "0"+self.dString
#產生下載日期
self.date = self.yString+self.mString+self.dString
#產生下載檔名
self.savefile = os.path.normpath(self.path+"/"+self.date+".jpg")
#圖片已存在或已下載就略過
if os.path.isfile(self.savefile):
#print self.savefile+" 已存在."
continue
else:
#嘗試下載圖片
try:
self.buffer = StringIO()
self.c = pycurl.Curl()
self.c.setopt(self.c.URL, picUrl+self.date)
self.c.setopt(self.c.WRITEFUNCTION, self.buffer.write)
self.c.perform()
self.c.close()
except:
#下載圖片失敗
continue
else:
#圖片是"本日無出報"或圖片跟前天重複就不儲存了
if self.buffer.len < 19000 or self.buffer.len == self.lastLen:
#print self.savefile+" buffer.len="+str(self.buffer.len)+ \
# " lastLen="+str(self.lastLen)
continue
#儲存圖片
try:
self.buffer.seek(0)
self.lastLen = self.buffer.len
self.im = Image.open(self.buffer)
self.im.save(self.savefile, 'JPEG')
except:
#儲存圖片失敗
print time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime())+" 儲存 "+self.savefile+" 失敗."
pass
else:
#儲存圖片成功
#print time.strftime("%Y-%m-%d %H:%M:%S",
# time.localtime())+" 儲存 "+self.savefile+" 成功."
pass
time.sleep(0.1)
def main():
global now, PP, picUrl, path
#今天日期
now = datetime.datetime.now().timetuple()
#年份範圍
PP = range(1951, now.tm_year+1)
#下載網址
picUrl = r"https://event.franklin.com.tw/C2014_11_TGF/showimg.aspx?date="
#存檔路徑
path = os.path.normpath(os.path.dirname(__file__)+"/pic")
#存檔目錄不存在
if not os.path.isdir(path):
try:
os.makedirs(path)
except:
print "產生存檔目錄 "+path+" 失敗."
sys.exit()
else:
print "存檔目錄 "+path+" 不存在,產生新目錄."
print "圖片將儲存於 "+path+" 目錄."
#啟動 Thread
for i in xrange(1, 6):
GetPic('ThreadFun', i).start()
time.sleep(0.1)
if __name__ == '__main__':
main()
---------------------------------------------------------------------
--
標題 [討論]
為什麼女生殖器要叫做"雞掰"?
我翻了諸多成人歷史書籍也沒看過有人寫"雞掰"
→ darkmanx2:http://0rz.tw/CIAJQ 08/11 21:43
幹,我親戚都在旁邊,你害死我了
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.132.215.155
※ 文章網址: http://www.ptt.cc/bbs/Python/M.1420436445.A.4E3.html
推
01/05 13:47, , 1F
01/05 13:47, 1F
推
01/05 15:33, , 2F
01/05 15:33, 2F
推
01/05 15:41, , 3F
01/05 15:41, 3F
→
01/05 15:42, , 4F
01/05 15:42, 4F
※ 編輯: taxi550 (220.132.215.155), 01/05/2015 17:50:35
推
01/05 18:44, , 5F
01/05 18:44, 5F
※ 編輯: taxi550 (220.132.215.155), 01/06/2015 00:41:21
※ 編輯: taxi550 (220.132.215.155), 01/06/2015 18:42:42
※ 編輯: taxi550 (220.132.215.155), 01/06/2015 18:46:18
推
01/07 13:21, , 6F
01/07 13:21, 6F
推
01/11 12:43, , 7F
01/11 12:43, 7F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
心得
6
7