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))
相關文章
相關標籤/搜索