Re: [心得] 取得聯合報歷史頭版

看板Python作者 (Beethoven Virus)時間10年前 (2015/01/05 13:09), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
我給個有點偷懶的做法, 但並沒有要比對圖片XD 只是用 async 的方式去快速存取每個link, 這邊預設程式執行下有 pic folder 然後 begin_day 跟 end_day 自己改XD (懶得寫讀外部參數了) import os import grequests from datetime import datetime from datetime import timedelta def main(): folder = "pic/" date_format = "%Y%m%d" url = r"https://event.franklin.com.tw/C2014_11_TGF/showimg.aspx?date=" begin_day = datetime.strptime('19510915', date_format) end_day = datetime.strptime('19510920', date_format) day_count = (end_day - begin_day).days + 1 urls = ['{0}{1}'.format(url, day.strftime(date_format)) \ for day in (begin_day + timedelta(n) for n in xrange(day_count))] rs = (grequests.get(u, stream=True) for u in urls) for pic in grequests.map(rs): if pic.status_code == 200: date = pic.url.split('date=')[1] if not os.path.isfile('{0}/{1}.png'.format(folder, date)): print "{0}.png will be stored in {1}".format(date, folder) with open('{0}/{1}.png'.format(folder, date), 'wb') as f: for chunk in pic.iter_content(): f.write(chunk) else: print "{0}.png already stored in {1}".format(date, folder) pic.close() if __name__ == '__main__': main() ※ 引述《taxi550 (小姐到哪)》之銘言: : 聯合報有個網站可以看到各年的頭版,分享抓取的程式,有人可改進嗎? : #!/usr/bin/env python : #!coding=utf-8 : import os : import time : import math : import pycurl : import operator : from PIL import Image : from StringIO import StringIO : def main(): : picUrl = r"https://event.franklin.com.tw/C2014_11_TGF/showimg.aspx?date=" : path = os.path.normpath(os.path.dirname(__file__)+"/pic") : #讀取"本日無出報"圖片 : try: : noPic = Image.open(os.path.join(os.path.dirname(__file__), "no.jpg")) : except: : #讀取"本日無出報"圖片失敗 : sw = None : else: : sw = True : noH = noPic.histogram() : if os.path.isdir(path): : pass : else: : os.makedirs(path) : print "目錄 "+path+" 不存在,產生新目錄." : print "圖片將儲存於 "+path+" 目錄." : for y in xrange(1951, 2016): : y = str(y) : for m in xrange(1, 13): : if m < 10: : #1951年9月16日前資料不存在所以跳過,寫法不是很好,將就一下 : if m < 9 and y == "1951": : continue : m = "0"+str(m) : else: : m = str(m) : print "開始擷取 "+y+"年"+m+"月." : for d in xrange(1, 32): : if d < 10: : date = y+m+"0"+str(d) : else: : date = y+m+str(d) : savefile = os.path.normpath(path+"/"+date+".jpg") : #print savefile : #圖片已存在或已下載就略過 : if os.path.isfile(savefile): : print savefile+" 已存在." : continue : else: : #嘗試取得圖片 : try: : buffer = StringIO() : c = pycurl.Curl() : c.setopt(c.URL, picUrl+date) : c.setopt(c.WRITEFUNCTION, buffer.write) : c.perform() : c.close() : except: : #取得圖片失敗 : continue : else: : try: : buffer.seek(0) : im = Image.open(buffer) : if sw: : imH = im.histogram() : #比對圖片,數字越大說明相差的越大,相似度100%接近860 : rms = math.sqrt(reduce(operator.add, : list(map(lambda a,b: (a-b)**2, noH, imH)))/len(noH)) : #print rms : if rms > 870: : #圖片不相同 : im.save(savefile, 'JPEG') : else: : #圖片比對相同就跳過 : continue : else: : im.save(savefile, 'JPEG') : except: : continue : else: : #圖片儲存成功 : print time.strftime("%Y-%m-%d %H:%M:%S", : time.localtime())+" 儲存 "+savefile+" 成功." : if __name__ == '__main__': : main() -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.171.160.236 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1420463387.A.93D.html
文章代碼(AID): #1KgeqRaz (Python)
文章代碼(AID): #1KgeqRaz (Python)