def main(): inputfile = "new.html" outputfile = "bar.txt" htmlLines = getHtmlLines(inputfile) imageUrls = extractImageUrls(htmlLines) showResults(imageUrls) saveResults(outputfile,imageUrls) def getHtmlLines(htmlpath): f = open(htmlpath,"r",encoding="utf-8") ls = f.readlines() f.close() return ls def extractImageUrls(htmllist): urls = [] for line in htmllist: if 'img' in line: url = line.split('src=')[-1].split('"')[1] if 'https' in url: urls.append(url) #print(urls) return urls def showResults(urls): count = 1 for url in urls: #print("第{ }个URL:{ }".format(count,url)) print(url) count += 1 def saveResults(filepath,urls): f = open(filepath,"w") for url in urls: f.write(url+ "\n") f.close() main()