php 抓取网页生成图片( 2021-06-01 )

优采云 发布时间: 2021-11-11 08:10

  php 抓取网页生成图片(

2021-06-01

)

  Python爬虫爬取网页图片

  时间:2021-06-01

  本文章为大家介绍Python爬虫爬取网页图片,主要包括Python爬虫爬取网页图片的使用、应用技巧、基础知识点总结及注意事项。有一定的参考价值,有需要的朋友可以参考。一度。

  通过python实现这样一个简单的爬虫功能,就可以将我们想要的图片爬到本地了。

  我们用python来实现这样的功能。

  

# -*- coding: utf-8 -*-

""" 爬取图片 """

import urllib

import re

import time

import os

# 显示下载进度

def schedule(a, b, c):

'''''

a:已经下载的数据块

b:数据块的大小

c:远程文件的大小

'''

per = 100.0 * a * b / c

if per > 100:

per = 100

print '%.2f%%' % per

def getHtml(url):

page = urllib.urlopen(url)

html = page.read()

return html

def downloadImg(html):

reg = r'src="(.+?\.jpg)" pic_ext'

imgre = re.compile(reg)

imglist = re.findall(imgre, html)

# 定义文件夹的名字

t = time.localtime(time.time())

foldername = str(t.__getattribute__("tm_year")) + "-" + str(t.__getattribute__("tm_mon")) + "-" + str(

t.__getattribute__("tm_mday"))

picpath = 'C:\\Users\\Administrator\\PycharmProjects\\pythonProject\\1\\2\\%s' % (foldername) # 下载到的本地目录

if not os.path.exists(picpath): # 路径不存在时创建一个

os.makedirs(picpath)

x = 0

for imgurl in imglist:

target = picpath + '\\%s.jpg' % x

print 'Downloading image to location: ' + target + '\nurl=' + imgurl

image = urllib.urlretrieve(imgurl, target, schedule)

x += 1

return image;

if __name__ == '__main__':

print ''' *************************************

** Welcome to use Spider **

** Created on 2021-06-01 **

** @author: cruise **

*************************************'''

html = getHtml("http://tieba.baidu.com/p/2460150866")

downloadImg(html)

print "Download has finished."

转载自https://blog.csdn.net/cruise_h/article/details/25737737

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线