Detailed example of Python implementation of simple web page image capture

小云云
Release: 2017-12-18 10:52:09
Original
4046 people have browsed it

This article mainly introduces the complete code example of simple web page image grabbing in Python. It has certain reference value and friends in need can refer to it.

The steps to use python to capture network images are:
1. Obtain the web page source code according to the given URL
2. Use regular expressions to filter out the image addresses in the source code
3. Download network images based on the filtered image address

The following is a relatively simple implementation of grabbing images from a certain Baidu Tieba webpage:


# -*- coding: utf-8 -*- 
# feimengjuan 
import re 
import urllib 
import urllib2 
#抓取网页图片  
#根据给定的网址来获取网页详细信息,得到的html就是网页的源代码 
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 = imgre.findall(html) #表示在整个网页中过滤出所有图片的地址,放在imglist中 
  x = 0 
  for imgurl in imglist: 
    urllib.urlretrieve(imgurl,'%s.jpg' %x) #打开imglist中保存的图片网址,并下载图片保存在本地 
    x = x + 1 
 html = getHtml("http://tieba.baidu.com/p/2460150866")#获取该网址网页详细信息,得到的html就是网页的源代码 
getImg(html)#从网页源代码中分析并下载保存图片
Copy after login

Further organized the code and created a "pictures" folder locally to save the pictures


# -*- coding: utf-8 -*- 
# feimengjuan 
import re 
import urllib 
import urllib2 
import os 
#抓取网页图片  
#根据给定的网址来获取网页详细信息,得到的html就是网页的源代码 
def getHtml(url): 
  page = urllib.urlopen(url) 
  html = page.read() 
  return html 
 
#创建保存图片的文件夹 
def mkdir(path): 
  path = path.strip() 
  # 判断路径是否存在 
  # 存在  True 
  # 不存在 Flase 
  isExists = os.path.exists(path) 
  if not isExists: 
    print u'新建了名字叫做',path,u'的文件夹' 
    # 创建目录操作函数 
    os.makedirs(path) 
    return True 
  else: 
    # 如果目录存在则不创建,并提示目录已经存在 
    print u'名为',path,u'的文件夹已经创建成功' 
    return False 
# 输入文件名,保存多张图片 
def saveImages(imglist,name): 
  number = 1 
  for imageURL in imglist: 
    splitPath = imageURL.split('.') 
    fTail = splitPath.pop() 
    if len(fTail) > 3: 
      fTail = 'jpg' 
    fileName = name + "/" + str(number) + "." + fTail 
    # 对于每张图片地址,进行保存 
    try: 
      u = urllib2.urlopen(imageURL) 
      data = u.read() 
      f = open(fileName,'wb+') 
      f.write(data) 
      print u'正在保存的一张图片为',fileName 
      f.close() 
    except urllib2.URLError as e: 
      print (e.reason) 
    number += 1  
#获取网页中所有图片的地址 
def getAllImg(html): 
  #利用正则表达式把源代码中的图片地址过滤出来 
  reg = r'src="(.+?\.jpg)" pic_ext' 
  imgre = re.compile(reg) 
  imglist = imgre.findall(html) #表示在整个网页中过滤出所有图片的地址,放在imglist中 
  return imglist   
#创建本地保存文件夹,并下载保存图片 
if __name__ == '__main__': 
  html = getHtml("http://tieba.baidu.com/p/2460150866")#获取该网址网页详细信息,得到的html就是网页的源代码 
  path = u'图片' 
  mkdir(path) #创建本地文件夹 
  imglist = getAllImg(html) #获取图片的地址列表 
  saveImages(imglist,path) # 保存图片
Copy after login

As a result, dozens of files were saved in the "pictures" folder Pictures, such as screenshots:

Related recommendations:

PHP simple web page image grabbing class

How to capture the images here to local

Example of PHP+Ajax remote image grabber download

The above is the detailed content of Detailed example of Python implementation of simple web page image capture. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template