Python抓取网站图片

使用Python抓取网站图片

#!/usr/bin/env python
#coding:utf8
import re,urllib
url='//linuxeye.com/'
def getHtml():
    page = urllib.urlopen(url)
    html = page.read()
    return html

def getImg(html):
    reg = r'<img src="(.*?\.\w{3,4})"'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    n = 0
    print imglist
    for imgurl in imglist:
        n=n+1
        pic_suffix = imgurl.split('.')[-1]
        if imgurl[0:4] == 'http':
            print imgurl
            urllib.urlretrieve(imgurl,'%d.%s' % (n,pic_suffix))
        else:
            print url + imgurl
            urllib.urlretrieve(url + imgurl,'%d.%s' % (n,pic_suffix))

U=getHtml()
getImg(U)

Wed Jun 26 18:12:02 CST 2013


【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps

【AD】搬瓦工限量套餐:POWERBOX-30-1536,美国洛杉矶DC99 CN2 GIA,年付仅$41.95!