python3 crawling picture example code

  • 2021-01-22 05:16:07
  • OfStack

The specific code is as follows:


#coding=utf8
from urllib import request
import re
import urllib,os
url='http://tieba.baidu.com/p/3840085725'
def get_image(url):
  # Get the page source code 
  page = urllib.request.urlopen(url)
  html = page.read()
  # Decode, otherwise report an error 
  html = html.decode('utf8')
  # The regular match gets the content of () 
  reg = r'src="(https.+?.[jpg,png])"'
  imge = re.compile(reg)
  #  Get regular matched data, "(.+?.jpg)"  The data is returned 1 a list
  imglist = imge.findall(html)
  return imglist
def save_img(imglist):
  dir = os.path.join(os.path.dirname(__file__),'img')
  i=1
  for img in imglist:
    #python3 Formatts another string 1 Kind of writing 
    imgpath = f'{dir}\\image{i}.jpg'
    try:
      #urlretrieve Download the image and save it locally 
      urllib.request.urlretrieve(img,imgpath)
      i += 1
      print(u' Image starts to download ')
    except Exception:
      print(f'image:{img} Download failed ')
      continue
imglist = get_image(url)
save_img(imglist)

conclusion


Related articles: