综合应用题
参照代码模板完善代码,实现下述功能。
文件ngchina.html 保持了网页源代码,请将该页面中图片的URL 提
取出来,并输出所有图像的URL。
习题讲解
#P301
#读取HTML 文件内容def getHTMLlines(htmlpath):
f = open(htmlpath,"r",encoding = 'utf-8')
ls = f .readlines()
f .close()
return ls
#用于解析文件并提取图像的URLdef extractImageUrls(htmllist):
urls = []
for line in htmllist:
if 'img' in line:
url = line.split('src=')[-1].split('"')[1]
if 'http' in url:
urls.append(url)
return urls
#将获取的链接输出到屏幕上def showResults(urls):
count = 1
for url in urls:
print("第{:2}个URL:{}".format(count,url))
count += 1
# 主程序:1 读取文件;2 解析并提取其中的图片链接;3 输出提取结果到屏幕def
main():
inputfile = "ngchina.htm