[python]Python 㧠ç»åã¹ã¯ã¬ã¤ãã³ã°
çªç¶ã§ããããã¾ã¾ã§ã¡ãã£ã¨ããã¡ã¢æ¸ãã hiki ãããã®ããã°ã«æ¸ãã¦ããããã§ãããEmacs Wikiã使ãããã«ãªã£ã¦ãããã便å©ããã¦ãã¡ã¢æ¸ãã¯å ¨é¨ Emacs-Wikiã«æ¸ãããã«ãªã£ã¦ãã¾ãã¾ããã
ãããªããã§ããã ã§ãããã¾ãæ´æ°ãããªããã®æ¥è¨ã®æ´æ°é »åº¦ãããã«å°ãªããªã£ã¦ãã¾ãããã§ããããã¾ã Wiki ã«æ¸ãã¦ããã¨ããå ¬éããã°ãããã§ããã©ãããã¾ãããããããã§ãæ¥è¨æ¸ããã¿ã®ããã«ãPython ã® Mechanize ã§éãã§ã¿ã¾ããã
ã¾ããã¤ã³ã¹ãã¼ã«ã
sudo portinstall -P www/py-mechanize
ports ã«ãã£ãã®ã§ãeasy_install ã¯ä½¿ãã¾ããã§ããããã¼ããFreeBSDã¯æ¥½ã§ããã
ãã¦ãããMechanize ã使ã£ã¦ä½ãæ¸ãããã¨æ©ãã§ããã¨ãããããããã°ãæããããããããã°ã®ç»åãä¸æ°ã«ãã¦ã³ãã¼ãããã¹ã¯ãªããããã£ããªããããã¨æãåºããã¡ãã£ã¨æ¤ç´¢ãã¦ã¿ããã©è¦ã¤ããããã¡ããã©ããã®ã§ãç»åã¹ã¯ã¬ã¤ãã³ã°ã®ã¹ã¯ãªãããæ¸ãã¦ã¿ããã¨ã«ããã
# -*- coding:euc-jp -*- import mechanize import urllib2 import time import datetime def download_image(url): filename = url.split("/")[-1] dat = urllib2.urlopen(url) open(filename,"wb").write(dat.read()) print "[%s] %s was downloaded." % (datetime.datetime.now().strftime("%H:%M:%S"), filename) time.sleep(1) br = mechanize.Browser() br.open('http://blog.livedoor.jp/akanehotaru/archives/cat_736051.html') next_page = unicode('次ã®ãã¼ã¸ã¸', 'euc-jp').encode(br.encoding()) while True: for link in br.links(url_regex="image"): download_image(link.url) try: br.follow_link(text_regex=next_page) except: break
ãããããæèªãã¦ãããã°ã«ããåçããã¾ã¨ãã¦ãã¦ã³ãã¼ããã¦ãããã¹ã¯ãªããã§ãããããããããã¨ãè¨ã£ã¨ãã¦ããããããããããã¾ããããããããããã