欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python — 获取网页图片资源

程序员文章站 2022-05-06 14:05:48
...

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)

相关标签: 获取网络资源