Use python to batch download and paste pictures with source code,

Source: Internet
Author: User
Tags url example

Use python to batch download and paste pictures with source code,

 

Environment: windows 7 64-bit; python2.7; IDE pycharm2016.1

 

Function:

Batch download all images in all posts on a certain page of Baidu Post Bar

 

Usage:

1. Install python2.7, re module, and urllib2 module.

2. Copy the following source code and save it as the tbImgiDownloader. py file.

3. Open a post and copy its URL

4. open the file tbImgiDownloader. py and enter the URL in the single quotation marks of row 37th. Save

5. Double-click tbImgiDownloader. py.

 

Note:

1. This program can download images from about 50 posts each time.

2. Image names are automatically saved as time + order

3. If you cannot run it, contact us.

4. Do not copy the line number when copying the source code (I just did this-_-|)

5. Don't forget to recommend it to anyone who thinks it is easy to use!

1 #! /Usr/bin/env python 2 # coding = UTF-8 3 4 import re, time 5 import urllib2, urllib 6 7 8 def tiebaImgiDownloader (url ): 9 ''' 10. jpg Format Image download tool: 11 format parameter the url address of a post 12 will save the image to the current directory 13 '''14 15 pattern = r'img class = "BDE_Image ". *? Src = "(.*? Jpg) "'# Regular Expression of the link to be crawled 16 fstr = urllib2.urlopen (url ). read () # read the Post webpage source code str to fstr17 urllist = re. findall (pattern, fstr) # crawl all jpg links that match the Regular Expression and save them in urllist 18 urllist = list (set (urllist )) 19 20 print 'total crawled % d image links' % len (urllist), '\ n' 21 22 I = 123 for furl in urllist: 24 timestr = time. strftime ('% Y % m % d % H % M % s') 25 urllib.urlretrieve(furl,timestr;'0368d.jpg' % I) # download images one by one, and name it "current time + ordinal 26 print" saved image ", timestrpolic'01_d.jpg \ n' % i2 7 I + = 128 29 print 'the image has been downloaded! \ N \ n' 30 31 return True32 33 34 def _ main __(): 35 print '\ n \ t welcome to use the post bar jpg Format Image download tool! \ N' 36 37 html = urllib. urlopen (''). read () # read the source code of a page ................. Paste the URL area ............................ 38 ''' URL example 39 1. http://tieba.baidu.com/f? Kw = % BE % CF % E6 % BA % B5t & fr = ala0 & loc = rec 40 2. http://tieba.baidu.com/f? Kw = % E9 % 9E % A0 % E5 % A9 % A7 % E7 % A5 % 8E & ie = UTF-8 & pn = 200 小 41 3. http://tieba.baidu.com/f? Kw = % E5 % A3 % 81% E7 % BA % B8 & ie = UTF-8 & tab = good boutique wallpaper 42 ''' 43 pattern = r'a href = "(. p. [0-9] *) "'# Regular Expression 44 urllist = re. findall (pattern, html) # capture all second-level webpage URLs and return list45 urllist = list (set (urllist) # Delete duplicate second-level webpage URLs 46 preurl = r'http: // tieba.baidu.com '# prefix of the second-level Web site website 47 print' capture % d second-level Web pages \ n' % len (urllist) 48 49 for urlOne in urllist: 50 tiebaImgiDownloader (preurl + urlOne) # download the image 51 52 return 053 54 55 if _ name _ = '_ main _' on the second-level webpage __': 56 _ main __()

Postscript: This is my original article. Please indicate the source for reprinting. Thank you for your cooperation.

 

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.