# Import from the monky genent module ① from GEVENT Import Monkey # coroutine into the program run ② monkey.patch_all () Import GEVENT, requests, Time # import requests and Time Start the time.time = () # start recording the program time URL_LIST = [ 'https://www.baidu.com/', 'https://www.sina.com.cn/', 'http://www.sohu.com/', 'HTTPS: // www.qq.com/ ', ' https://www.163.com/ ', ' http://www.iqiyi.com/ ', ' https://www.tmall.com/ ', ' HTTP: //www.ifeng.com/ '] # encapsulated into the list of eight sites DEF get_Data (URL): R & lt requests.get = (URL) # with requests.get () function crawling website print (url, time.time () -start, r.status_code) task_list = [] # Create an empty list for url in url_list: # Create a task get_data with gevent inside the spawn function method name, url parameter ③ Task = gevent.spawn (get_data, url) # add tasks to create task_list④ task_list.append (Task) all perform the task in the task list # ⑤ gevent.joinall (task_list) end = time.time () # recording program end time Print (end-start) # end-start is the end time minus the start time is the ultimate time spent.
Using the queue, as follows:
# Import module monky from the genent ① from gevent Import Monkey # ② run the program into the coroutine monkey.patch_all () Import gevent, Requests, Time # Import module from the queue gevent library from gevent.queue Import Queue # Import requests and time start the time.time = () # recording program start time URL_LIST = [ 'https://www.baidu.com/', 'https://www.sina.com.cn/', 'HTTP: / /www.sohu.com/ ', ' https://www.qq.com/ ', ' https://www.163.com/ ', ' http://www.iqiyi.com/ ', ' HTTPS : //www.tmall.com/ ', ' http://www.ifeng.com/ '] # encapsulated in the eight sites list # create a queue object, and assigned to work. = Queue Work () for url in URL_LIST: # With put_nowait () function you can put URLs into the queue. work.put_nowait (url) get_Data DEF (): # When the queue is not empty, on the implementation of the following procedure. not work.empty the while (): # with get_nowait () function can be put in the queue of URLs removed. = work.get_nowait URL () R & lt requests.get = (URL) # with requests.get () function qsize site crawling queue length Print (URL, work.qsize (), r.status_code) task_list = [] # Create a empty list # created two crawlers for the X-in the Range (2): # create a task get_data with gevent inside the spawn function method name task = gevent.spawn (get_data) # create task is added to task_list④ task_list.append (task ) all tasks ⑤ # perform the task list gevent.joinall (task_list) end = time.time () # recording program end time Print (end-start) # end-start is the end time minus the start time is spent on final time.