python + + multi-process regular crawling embarrassments Encyclopedia Images

Man of few words said, directly on the code;

# Required libraries 
Import Requests
 Import Re
 Import OS
 from multiprocessing Import Pool
 # request header 
headers = {
     ' the User-- Agent ' : ' the Mozilla / 5.0 (the Windows NT 6.1; the WOW64) AppleWebKit / 537.36 (KHTML, like the Gecko) the Chrome / 65.0. Safari 3325.181 / 537.36 ' 
} 

# main function 
DEF get_img (URL):
     # custom image storage path 
    img_path = ' ./img/ ' 
    IF  Not os.path.exists (img_path): 
        os.mkdir (img_path) 
    the try:
         # Request the page 
        the Response = requests.get (url = url, headers = headers)
         # regular extract pictures address 
        the Response = re.findall ( ' <div class = "Thumb" * <img src = "(*).?.? .? "* </a> ' , response.text, re.S)
         # loop picture address 
        for i in the Response:
             # stitching complete picture of the route 
            url = ( ' HTTP: ' + i)
             # request routing complete picture of 
            the Response = requests .get (url, headers)
             # picture naming 
            img_name = url.split ( '/ ' ) [--1 ]
             # determines whether the image has been downloaded 
            IF os.path.exists (img_path + img_name):
                 Print ( ' image already exists ' )
             the else :
                 # Photo 
                with Open (img_path + img_name, ' WB ' ) AS F : 
                    f.write (response.content) 
                    Print ( ' downloading: ' + URL)
     the except Exception AS E:
         Print (E)
 # main entry 
IF  the __name__ ==' __Main__ ' :
     # configured ip address all 
    URLs = [ ' https://www.qiushibaike.com/imgrank/page/{}/ ' .format (I) for I in Range (1,14 )]
     # use multiple processes 
    = the pool Pool ()
     # open multi-process crawling 
    pool.map (get_img, urls)
     Print ( ' downloaded ' )

downloading;

Open folder to view pictures;

 

done

 

Guess you like

Origin www.cnblogs.com/nmsghgnv/p/11308354.html