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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章