# Multi-threaded to write six Web sites into a file (parallel)
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 = [] # Store each thread
Start_time = Time.time ()
For URL in URLs: # Loop each URL
new_file = '/http ' + URL
file_name = URL + '. html '
t = Threading. Thread (target = Write_html,args = (new_url,file_name)) # instantiates a thread
Lis.append (t)
T.start () # Start a thread
# Start six threads, let them run, the main thread waits for them, join is the main thread waiting for each sub-thread to finish executing
For obj in Lis:
Obj.join () # Join must be placed outside of the six threads that are started
End_time = Time.time ()
Print (' program runs in total ', end_time-start_time)
The main thread waits for the child thread to crawl all the pages and then the main thread to send the message
day10_ Multithreading 3