Ideas:
1. Look at the Web page and find the IMG tag
2. Extracting IMG tags from web pages via requests and BS libraries
3. Grab the img tag, then extract the src inside, then download the image.
4. Download the image and put it in the folder by Urllib Urllib.urlretrieve (the first preparation is to get the current path and create a new folder)
5. If you have more than one picture, keep repeating 3-4
Because the crawler writes less, through own debugging, finally wrote out
Directly below the code:
1 #coding = ' Utf-8 '2 ImportRequests3 fromBs4ImportBeautifulSoup4 ImportUrllib5 ImportOS6 ImportSYS7 Reload (SYS)8Sys.setdefaultencoding ("Utf-8")9 Ten if __name__=='__main__': OneURL ='http://www.qiushibaike.com/' Ares =requests.get (URL) -Res.encoding ='Utf-8' -Soup = BeautifulSoup (Res.text,'Html.parser') theIMGs = Soup.find_all ("img") - -_path =OS.GETCWD () -New_path = Os.path.join (_path,'Pictures') + if notOs.path.isdir (new_path): - Os.mkdir (New_path) +New_path + ='\ ' A at Try: -x = 1 - ifIMGs = = []: - Print "done!" - forImginchIMGs: -link = img.get ('src') in if 'http' inchLink: - Print "It ' s downloading%s"%x +"th ' s piture" toUrllib.urlretrieve (link, New_path +'%s.jpg'%x) +x + = 1 - the exceptException, E: * Printe $ Else:Panax Notoginseng Pass - finally: the ifx: + Print "It ' s done!!!"
The next result:
Summarize:
Although the initial thinking is not clear, and how to save the picture, are not very familiar
But after their own thinking, as long as the idea clear, determined the direction is good to do, as for the function will not be used, you can directly Baidu check, very convenient
In a word, before writing a program must have ideas, while writing side thinking is not good, so easy to rework
But in the end it was written out, haha
We also invite you to study together and correct me.
----------------------
If reproduced, please indicate the source Oh, thank you
Python crawler captures embarrassing pictures and stores them in a local folder