Home  >  Article  >  Backend Development  >  Implementation method of downloading images in Python (super simple)

Implementation method of downloading images in Python (super simple)

不言
不言Original
2018-04-16 17:00:552965browse

This article introduces the implementation method of downloading images in python (super simple), which has certain reference value. Now I share it with you. Friends in need can refer to it.

The editor below will explain it to you. Here is an article on how to implement downloading images in python (super simple). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.

We sometimes need to find and download pictures on the Internet. When the number is relatively small, right-click to save, and the picture can be downloaded easily. However, some pictures cannot be downloaded. If you have set special settings, right-clicking does not display the save option, or you need to download a lot of pictures, this situation can be easily solved by writing a Python crawler code!

1. Page crawling


#coding=utf-8
  import urllib
  def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html
  html = getHtml("https://tieba.baidu.com/p/5582243679")
  print html


The page data crawling process defines the getHtml() function, whose function is to pass a URL to getHtml() and ultimately download the entire page.

2. Page data filtering


import re
  import urllib
  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 = re.findall(imgre,html)
    return imglist
  html = getHtml("https://tieba.baidu.com/p/5582243679")
  print getImg(html)


In page data filtering, a new function getImg() is defined , the function of this function is to filter out image addresses in .jpg format.

3. Image 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 = re.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)


Obtain all qualified image URLs through a for loop and use urllib. The urlretrieve() method downloads the remote data to the local and renames it!

The following is a supplement

as follows:


import urllib.request
response = 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() brackets can be either a string or a request object. When the string is passed in, it will be converted into a request object, so the code

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

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

1. response = urllib.request.urlopen(req)
2. response also has geturl, info, getcode methods


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 recommendations:

Python simply calculates the file MD5 value Method example

Python simple method to control the computer


##

The above is the detailed content of Implementation method of downloading images in Python (super simple). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn