码迷,mamicode.com
首页 > 编程语言 > 详细

python3 抓取图片

时间:2017-11-02 00:04:21      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:return   dal   mgr   retrieve   eve   ext   lib   baidu   jpg   

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 抓取图片

标签:return   dal   mgr   retrieve   eve   ext   lib   baidu   jpg   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!