Python download Image implementation method (super Simple)

Source: Internet
Author: User
This article introduces the content of the Python download Image implementation method (ultra-simple), has a certain reference value, now share to everyone, the need for friends can refer to

The following small series for everyone to bring a python download image implementation method (super Simple). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

We sometimes need to find and download images on the Internet, when the number is less, right-click to save, it is easy to achieve the download of images, but some pictures have been special settings, right click does not show the save options, or need to download a lot of pictures, such a situation, Write a Python crawler code can be easily solved!

First, page crawl


#coding =utf-8 Import urllib def gethtml (URL):  page = urllib.urlopen (URL)  html = page.read ()  return htm L HTML = gethtml ("https://tieba.baidu.com/p/5582243679") Print HTML


The page data fetching process defines the gethtml () function, which is the function of passing a URL to gethtml () and finally downloading the entire page.

Second, the page data filtering


Import re import urllib def gethtml (URL):  page = urllib.urlopen (URL)  html = page.read ()  return HTML D EF getimg (HTML):  reg = R ' src= "(. +?\.jpg)" Pic_ext '  Imgre = Re.compile (reg)  imglist = Re.findall (imgre,ht ML)  return imglist HTML = gethtml ("https://tieba.baidu.com/p/5582243679") print getimg (HTML)


In page data filtering, a new function, getimg (), is defined, and the function is to filter out the image address of the. jpg format.

Third, Picture download


#coding =utf-8 Import urllib import re def gethtml (URL):  page = urllib.urlopen (URL)  html = page.read ()  return HTML def getimg (HTML):  reg = R ' src= "(. +?\.jpg)" Pic_ext '  Imgre = Re.compile (reg)  imglist = R E.findall (imgre,html)  x = 0  for Imgurl in imglist:    urllib.urlretrieve (Imgurl, '%s.jpg '% x)    x+= 1 HTML = gethtml ("https://tieba.baidu.com/p/5582243679") print getimg (HTML)


Get all eligible picture URLs with a for loop and use the Urllib.urlretrieve () method to download remote data locally and rename it!

Here are the supplemental

As shown below:


Import urllib.requestresponse = Urllib.request.urlopen (' http://www.jb51.net/g/500/600 ') cat_img = Response.read () With open (' cat_500_600.jpg ', ' WB ') as F:f.write (cat_img)


Urlopen () in parentheses can be either a string or a request object, which is converted into a Request object when the string is passed, so the code

Response = Urllib.request.urlopen (' http://www.jb51.net/g/500/600 ') can also be written

req = urllib.request.Request (' http://www.jb51.net/g/500/600 ')

1, Response = Urllib.request.urlopen (req)
2, Responce and Geturl,info,getcode method


Code with open (' cat_500_600.jpg ', ' WB ') as F:

F.write (cat_img) is equivalent to

1, f = open (' cat_500_600.jpg ', ' WB ')

2. Try:

3. Data = F.write (cat_img)

4. Finally:

5, F.close ()

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.