python — 獲取網頁圖片資源

Python 獲取網絡資源

# coding=UTF-8

import re
import urllib

def getHtml(url):
	page = urllib.urlopen(url)
	html = page.read()
	#html = html.decode('UFT-8')
	return html


def getImg(html):
    reg = r'src="(.*?\.jpg)"'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    print(imglist)
    
    x= 0
    for imgurl in imglist:
    	pathName="/Users/gjh/Desktop/圖片緩存文件/"+str(x)+".jpg"
    	urllib.urlretrieve(imgurl,pathName)
    	print("正在下載.......")
    	x+=1


#htmlStr = "https://max.book118.com/index.php?g=Home&m=NewView&a=index&aid=8057045117001121&v=20190819"
htmlStr = "http://localhost:63342/untitled/index_2.html?_ijt=soopna1lkuo4o7446ed9a6rc9a"
html = getHtml(htmlStr)
print(html)
getImg(html)

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