#导入库 import urllib.request #获取单个html文件内容 def getHtml(url): html=urllib.request.urlopen(url).read() return html #将html文件重新命名,保存到指定位置 def saveHtml(file_name,file_content): #注意Windows文件命名的禁用符,比如/ with open(file_name.replace('/','_')+".html","wb") as f: f.write(file_content) #写文件用bytes而不是str,所以要转码 #读取csv格式的文件,只读 f = open("commed.csv","r") count=1 #读取每一行的数据,每一行的数据逐一处理 for line in f: count=count+1 a=[] a.append(line.strip().split(",")) site=a[0][0] if len(site) > 10: #满足条件后,将读取的每一个网址信息,进行访问保存,重新命名保存到本地 title="commed\"+str(count)+"行"+a[0][1] html=getHtml(site) saveHtml(title,html) #打印输出确认是否保存成功,显示当前处理数据的行数 print("下载成功"+"===第"+str(count)+"行") #关闭文件读取函数 f.close()