Python implements a simple image acquisition crawler function example

  • 2020-06-07 04:51:28
  • OfStack

This article illustrates Python's simple image acquisition crawler function. To share for your reference, specific as follows:

Simple Python crawler to get photos on web pages


#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)
  return imglist
//  Web site address 
url = "http://tieba.baidu.com/p/3368048910?pn=2"
html = getHtml(url)
listimg = getImg(html)
x = 0
for imgAddress in listimg:
  print imgAddress
  urllib.urlretrieve(imgAddress, 'image%s.jpg' % x)
  x+=1

For more information about Python, please refer to Python Socket Programming Skills Summary, Python Data Structure and Algorithm Tutorial, Python Function Using Skills Summary, Python String Operation Skills Summary, Python Introduction and Advanced Classic Tutorial and Python File and Directory Operation Skills Summary.

I hope this article has been helpful in Python programming.


Related articles: