python3.7腳本---爬取網頁圖片

#!/usr/bin/python

import re
import urllib
import urllib.request   #python3中urlopen、urlritrieve都在request庫裏面了,因此要導入此庫

def htmlGet(url):
        page = urllib.request.urlopen(url)
        html = page.read()
        return html

def imgGet(html):
        res = r'src="(https.*?\.jpg)"'
        imgre = re.compile(res)
        imglist = re.findall(imgre,html.decode("utf-8")) #html不加後面的會報錯typeerror,由於編碼格式的變化,這裏須要指定一下
        x = 0
        for i in imglist:
                urllib.request.urlretrieve(i,"%s.jpg" % x)
                x+=1

html = htmlGet("http://***")
imgGet(html)
相關文章
相關標籤/搜索