# 多线程把六个网站写到文件里(并行)
import requests,time,threading
def write_html(url,name):
r = requests.get(url)
with open(name,‘w‘,encoding = ‘utf8‘) as f:
f.write(r.text)
urls = [‘www.nnzhp.cn‘,‘www.besttest.cn‘,‘www.imdsx.cn‘,‘sb.nnzhp.cn‘,‘bbs.besttest.cn‘,‘video.besttest.cn‘] #六个网站URL
lis = [] # 存放每个线程
start_time = time.time()
for url in urls: # 循环每个url
new_file = ‘http://‘ + url
file_name = url + ‘.html‘
t = threading.Thread(target = write_html,args = (new_url,file_name)) # 实例化一个线程
lis.append(t)
t.start() # 启动一个线程
# 启动六个线程,让他们跑着,主线程等他们,join就是主线程在等待每个子线程执行完成
for obj in lis:
obj.join() # join必须放在启动的六个线程外面
end_time = time.time()
print(‘程序总共运行了‘,end_time - start_time)
主线程等待的意思是让子线程把所有的页面都爬完,然后主线程发送邮件