# 一名代码爱好者
import urllib.request
import re
import os
import urllib
def get_html(url):
page = urllib.request.urlopen(url)
html_a = page.read()
return html_a.decode('utf-8')
def get_img(html):
reg = r'https://[^\s]*?\.jpg'
imgre = re.compile(reg)
imglist = imgre.findall(html)
x = 0
path = 'E:\\test'
if not os.path.isdir(path):
os.makedirs(path)
paths = path + '\\'
for imgurl in imglist:
urllib.request.urlretrieve(imgurl, '{0}{1}.jpg'.format(paths, x))
x = x + 1
print('图片开始下载..........')
return imglist
html_b = get_html("http://www.nipic.com/index.html")
print(get_img(html_b))
Python 爬取网站图片(实测有用)
于 2022-03-23 16:48:25 首次发布