文件ngchina.htm1保存了网页源代码,请将该页面中图片的URL提取出来,并输出所有图片的URL。输出格式如下: 第1个URL:http://image.ngchina.com.cn/2018/0829/20180829012548753.jpg第2

admin2019-09-02  32

问题 文件ngchina.htm1保存了网页源代码,请将该页面中图片的URL提取出来,并输出所有图片的URL。输出格式如下:
第1个URL:http://image.ngchina.com.cn/2018/0829/20180829012548753.jpg第2个URL:http://image.ngchina.com.cn/2018/0823/thumb_469_352_20180823121155508.Jpg

选项

答案参考程序: #读取HTML文件内容 def getHTMLlines(htmlpath): f=open(htmlpath,’’r’’,encoding=’utf一8’) ls=f.readlines() f.close() return ls #用于解析文件并提取图片的URL def extractlmageUrls(htmllist): urls=[] for line in htmllist: if’img’in line: url=line.split(’src=’)[-1].split(’’’’)[1] if’http’in url: urls.append(ur1) return urls #将获取的链接输出到屏幕上 def showResults(urls): count=1 for ur1 in urls: print(’’第{:2}个URL:{}’’.format(count,ur1)) count+=1 #主程序:1读取文件;2解析并提取其中的图片链接;3输出提取结果到屏幕 def main(): inputfile=’’ngchina.html’’ htmllines=getHTMLlines(inputfile) imageUrls=extractImageUrls(htmllines) showResults(imageUrls)

解析
转载请注明原文地址:https://jikaoti.com/ti/0n50FFFM
0

相关试题推荐
最新回复(0)