Python简单爬取图片实例

import urllib.request  #进行URL请求
import re   #正则表达式库

def getHtml(url):
    page=urllib.request.urlopen(url)
    html=page.read()
    html=html.decode("utf-8")
    return html

def getImg(html):
    reg = 'src="(.+?\.jpg)" alt='
    imgre = re.compile(reg)
    imglist = re.findall(imgre, html)
    x = 1
    for imgurl in imglist:   #下载图片
        urllib.request.urlretrieve(imgurl, '%s.jpg' % x)
        x+=1
    return imglist

html = getHtml("http://pic.yxdown.com/list/0_0_1.html")
print (getImg(html))

猜你喜欢

转载自my.oschina.net/u/3778090/blog/1825666