首页 > 编程语言 > 详细

python3 抓取图片

时间:2017-11-01 23:21:15      阅读:267      评论:0      收藏:0      [点我收藏+]
import re
import urllib.request
# import urllib
import os

def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html.decode(‘UTF-8‘)

def getImg(html):
reg = r‘src="(.+?\.jpg)" pic_ext‘ # 要加括号,作为元组返回,抓取淘宝的图片png(先看源码中图片的地址路径)reg = r‘data-lazy="(.+?\.png)" ‘
imgre = re.compile(reg)
imglist = imgre.findall(html)
x = 0
path = ‘D:\\pythonTest\\images‘
if not os.path.isdir(path):
os.makedirs(path)
paths = path + ‘\\‘ # 保存在test路径下
for imgurl in imglist:
urllib.request.urlretrieve(imgurl, ‘{}{}.jpg‘.format(paths, x))
x = x + 1

html = getHtml("http://tieba.baidu.com/p/2460150866") # 淘宝的:html = getHtml(r"http://www.taobao.com/")
getImg(html)

python3 抓取图片

原文:http://www.cnblogs.com/vsmart/p/7768944.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!