昨日刚刚学习到函数篇,想把函数应用起来,就把前几日写的简单使用函数优化了一下,思路更清晰
import urllib.request
import re
import os
import urllib
def getHtml(url):
headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0‘}
redown = urllib.request.Request(url, headers=headers)
req = urllib.request.urlopen(redown, timeout=2).read()
return req.decode(‘utf-8‘)
def getImg(html):
imare = re.compile("<img src=‘(.*?png)‘>")
imgList = imare.findall(html)
x = 0
path = ‘D:\\test‘
# 将图片保存到D:\\test文件夹中,如果没有test文件夹则创建
if not os.path.isdir(path):
os.makedirs(path)
paths = path + ‘\\‘ # 保存在test路径下
for imgurl in imgList:
# 打开imglist中保存的图片网址,并下载图片保存在本地,format格式化字符串
urllib.request.urlretrieve(imgurl, ‘{}{}.png‘.format(paths, x))
x = x + 1
return imgList
html = getHtml("http://www.dgtle.com")#获取该网址网页详细信息,得到的html就是网页的源代码
print (getImg(html))