ÓÃPythonдµÄͼƬ֩ÖëÈË
дÁ˸öͼƬ֩ÖëÈËÍæÍæ£¬×¥Á˼¸¸öÍøÒ³ÊÔÊÔ£¬¸Ð¾õ²»²»´í¡£ºËÐĵĴúÂë¿ÉÄÜ20ÐÐÒ²²»µ½£¬¼ò½àÃ÷ÁË£¬ÎûÎû¡£·Ï»°ÉÙ˵£¬´ä»¨£¬ÉÏ´úÂë~~
#coding=utf-8
import os
import sys
import re
import urllib
URL_REG = re.compile(r'(http://[^/\\]+)', re.I)
IMG_REG = re.compile(r'<img[^>]*?src=([\'"])([^\1]*?)\1', re.I)
def download(dir, url):
'''ÏÂÔØÍøÒ³ÖеÄͼƬ
@dir ±£´æµ½±¾µØµÄ·¾¶
@url ÍøÒ³url
'''
global URL_REG, IMG_REG
m = URL_REG.match(url)
if not m:
print '[Error]Invalid URL: ', url
return
host = m.group(1)
if not os.path.isdir(dir):
os.mkdir(dir)
# »ñÈ¡html,ÌáȡͼƬurl
html = urllib.urlopen(url).read()
imgs = [item[1].lower() for item in IMG_REG.findall(html)]
f = lambda path: path if path.startswith('http://') else \
host + path if path.startswith('/') else url + '/' + path
imgs = list(set(map(f, imgs)))
print '[Info]Find %d images.' % len(imgs)
# ÏÂÔØÍ¼Æ¬
for idx, img in enumerate(imgs):
name = img.split('/')[-1]
path = os.path.join(dir, name)
try:
print '[Info]Download(%d): %s'% (idx + 1, img)
urllib.urlretrieve(img, path)
except:
print "[Error]Cant't download(%d): %s" % (idx + 1, img)
def main():
if len(sys.argv) != 3:
print 'Invalid argument count.'
return
dir, url = sys.argv[1:]
download(dir, url)
if __name__ == '__main__':
# download('D:\\Imgs', 'http://www.163.com')
main()
Ïà¹ØÎĵµ£º
Ê×ÏÈ×ªÔØÒ»¶Î¹ØÓÚÍÆ¼öµÄѧϰpythonµÄÊé¼®£¨ÔÎÄÁ´½Ó£©£º
1.ѧϰ¡¶A Byte of Python¡·£¬ÖÐÎÄÒëÃûΪ¡¶Python¼òÃ÷½Ì³Ì£¨pdfÏÂÔØµØÖ·£©¡·£¬È«ÊéÏÂÀ´Ö»ÓÐ100¶àÒ³£¬¼òµ¥¸ÉÁ·£¬Í¨Ë×Ò×¶®£¬Êǹ«ÈϵÄPythonÈëÃŽ̡̳£ÎÒËäȻϵ½ÁË¡¶Python¼òÃ÷½Ì³Ì¡·pdf°æ£¬µ«ÊǾõµÃÆäÖеĴúÂë¿ÉÄÜÊÇÒòΪÒëÕßת»»ÎĵµÀàÐ͵ÄÔÒò£¬¸ñʽ±äµÃºÜÂÒ£¬ËùÒÔ× ......
And last here is the overload operators example:
# map() takes two (or more) arguments, a function and a list to apply the function to
# lambda can be put anywhere a function is expected
# map() calls lambada for every element in the self list
# since Vector has overloaded __getitem__ and __len_ ......
python×ÖµäÅÅÐò
1¡¢
×¼±¸ÖªÊ¶£º
ÔÚpythonÀ×ÖµädictionaryÊÇÄÚÖõÄÊý¾ÝÀàÐÍ£¬ÊǸöÎÞÐòµÄ´æ´¢½á¹¹£¬Ã¿Ò»ÔªËØÊÇkey-value¶Ô£º
È磺dict = {‘username’£º‘password’£¬‘database’£º‘master’}£¬ÆäÖБusername’ºÍ& ......
ÎҵĻ·¾³ÊÇ£ºLinux version 2.4.21-4.EL
(bhcompile@daffy.perf.redhat.com) (gcc version 3.2.3 20030502 (Red Hat
Linux 3.2.3-20)) #1 Fri Oct 3 18:13:58 EDT 2003 + Python2.6.4
±¾ÎĽáºÏÎÒ°²×°Ê±ºòµÄÎÊÌ⣬×Ü½á¶ø³É
Óû§Ä¿Â¼Èç/home/liuguanyu/ , ±£Ö¤Óû§ÓÐrootȨÏÞ
1£¬¿´¿´ÓÐûÓа²×°
&nbs ......
²Î¿¼Á´½Ó£ºhttp://www.woodpecker.org.cn/diveintopython/functional_programming/dynamic_import.html
Ò» ¶¯Ì¬µ¼ÈëÄ£¿é
PythonµÄimport²»ÄܽÓÊܱäÁ¿£¬ËùÒÔÓ¦¸ÃÓà __import__º¯ÊýÀ´¶¯Ì¬µ¼Èë¡£
ÈçϵĴúÂëÎÞ·¨Õý³£µ¼ÈëÄ£¿é
modules = ['OpenSSL', 'Crypto', 'MySQLdb', 'sqlite3', 'zope.interface', 'pyasn1', 'twisted ......