易截截图软件、单文件、免安装、纯绿色、仅160KB

用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类型转换、数值操作


2008-12-21
python类型转换、数值操作
关键字: python类型转换、数值操作
python类型转换
Java代码
函数                      描述   
int(x [,base ])    ......

python算法实践3 冒泡排序

#冒泡排序
def BubbleSort(mylist):
n = len(mylist)
i = 0
j = 0
bExchange = False
for i in range(1, n):
bExchange = False
j = n - 1
while j >= i:
if mylist[j] < mylist[j - 1]:
tmp = mylist[j]
myli ......

python算法实践6 堆排序

#堆排序
def Heapify(mylist, start, end):
left = 0
right = 0
maxv = 0
left = start * 2
right = start * 2 + 1
while left <= end:
maxv = left
if right <= end:
if mylist[left] < mylist[right]:
maxv = right
......

Python 字典使用例子


#!/usr/bin/env python
#coding=utf-8
def buildConnectionString(params):
    return ":".join(["%s=%s" %(k, v) for k, v in params.items()])
if __name__ == "__main__":
    myParams = {"server":"mpilgrim", \
               &nbs ......

Python安装MySQLDb模块的种种问题及解决


我的环境是: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 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号