pineapple丿夜曲

导航

Python入门学习例子——从Hao123获取图片

import urllib.request
import re

#获取html页面内容
def getHtml(url):

  data=urllib.request.urlopen(url)
  pageContent = data.read()
  return pageContent

#下载图片

def getImage(html):
  reg = r'url\((.*?\.png)\)'
  parttena = re.compile(reg)
  imageUrlList = re.findall(parttena, html.decode("utf-8"))
  x=0
  print("There are %d pictures" % len(imageUrlList))
  for i in imageUrlList:
    try:
      urllib.request.urlretrieve(i,"E:\image\%s.png" % x)
      x+=1
      print(i)
    except:
      continue

if __name__=="__main__":
  htmlContent = getHtml("https://www.hao123.com/")
  getImage(htmlContent)

posted on 2017-06-08 14:32  pineapple丿夜曲  阅读(99)  评论(0编辑  收藏  举报