Python crawler captures embarrassing pictures and stores them in a local folder

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.