python抓取网页图片示例(python爬虫)
发布时间:2021-06-10 18:43:15
#-*- encoding: utf-8 -*-
'''
Created on 2014-4-24
@author: Leon Wong
'''
import urllib2
import urllib
import re
import time
import os
import uuid
#获取二级页面url
def findUrl2(html):
re1 = r'http://tuchong.com/\d+/\d+/|http://\w+(?<!photos).tuchong.com/\d+/'
url2list = re.findall(re1,html)
url2lstfltr = list(set(url2list))
url2lstfltr.sort(key=url2list.index)
#print url2lstfltr
return url2lstfltr
#获取html文本
def getHtml(url):
html = urllib2.urlopen(url).read().decode('utf-8')#解码为utf-8
return html
#下载图片到本地
def download(html_page , pageNo):
#定义文件夹的名字
x = time.localtime(time.time())
foldername = str(x.__getattribute__("tm_year"))+"-"+str(x.__getattribute__("tm_mon"))+"-"+str(x.__getattribute__("tm_mday"))
re2=r'http://photos.tuchong.com/.+/f/.+\.jpg'
imglist=re.findall(re2,html_page)
print imglist
download_img=None
for imgurl in imglist:
picpath = 'D:\\TuChong\\%s\\%s' % (foldername,str(pageNo))
filename = str(uuid.uuid1())
if not os.path.exists(picpath):
os.makedirs(picpath)
target = picpath+"\\%s.jpg" % filename
print "The photos location is:"+target
download_img = urllib.urlretrieve(imgurl, target)#将图片下载到指定路径中
time.sleep(1)
print(imgurl)
return download_img
# def callback(blocknum, blocksize, totalsize):
# '''回调函数
# @blocknum: 已经下载的数据块
# @blocksize: 数据块的大小
# @totalsize: 远程文件的大小
# '''
# print str(blocknum),str(blocksize),str(totalsize)
# if blocknum * blocksize >= totalsize:
# print '下载完成'
def quitit():
print "Bye!"
exit(0)
if __name__ == '__main__':
print ''' *****************************************
** Welcome to Spider for TUCHONG **
** Created on 2014-4-24 **
** @author: Leon Wong **
*****************************************'''
pageNo = raw_input("Input the page number you want to scratch (1-100),please input 'quit' if you want to quit>")
while not pageNo.isdigit() or int(pageNo) > 100 :
if pageNo == 'quit':quitit()
print "Param is invalid , please try again."
pageNo = raw_input("Input the page number you want to scratch >")
#针对图虫人像模块来爬取
html = getHtml("http://tuchong.com/tags/%E4%BA%BA%E5%83%8F/?page="+str(pageNo))
detllst = findUrl2(html)
for detail in detllst:
html2 = getHtml(detail)
download(html2,pageNo)
print "Finished."


猜你喜欢
- 在一个网站,大量数据的前后端交互,JSON是最好的传递数据方式了。在Django中,使用JSON传输数据,有两种方式,一种是使用Python
- ppt要想完美的转pdf,图片,还是需要在windows下面来操作。1,安装python3.5.1下载地址Windows x86-64 ex
- Python实现对网易云音乐的数据进行一个数据清洗和可视化分析对音乐数据进行数据清洗与可视化分析关于数据的清洗,实际上在上一一篇文章关于抓取
- 简介Casbin是一个强大的、高效的开源访问控制框架,其权限管理机制支持多种访问控制模型。casbin 的主要特性包括:支持自定义请求的格式
- 有如下的代码:class p1:def __init__(self,a,b):print("init in p1")se
- 取得 Oracle 10g 安装程序,或从 Oracle 技术网(OTN)下载光盘映像。在评估阶段您可以免费下载和使用无技术限制的全功能 O
- 递归函数两大特点:1.能够调用函数自身2.至少有一个出口(结束函数自身调用)函数实现:def calnum(num): if n
- 命令首先数据库迁移的两大命令: python manage.py makemigrations & python manage.py
- 一般在Django的视图函数中使用request.POST来获取请求体,request.POST是QueryDict类,通常作为dict来使
- 使用百度地图报错BMap is not defined1、在index.html添加密钥申请:http://lbsyun.baidu.com
- 在一般问题的优化中,最速下降法和共轭梯度法都是非常有用的经典方法,但最速下降法往往以”之”字形下降,速度较慢,不能很快的达到最优值,共轭梯度
- scipy.misc.logsumexp函数的输入参数有(a, axis=None, b=None, keepdims=False, ret
- 在使用numpy数组的过程中时常会出现nan或者inf的元素,可能会造成数值计算时的一些错误。这里提供一个numpy库函数的用法,使nan和
- 前面两天带着大家换了一个口味,带着大家学习了pyecharts的原理和部分图形制作。今天我们继续回归带你学MySQL系列,带着大家继续学习M
- 本文实例讲述了sql server实现分页的方法。分享给大家供大家参考,具体如下:declare @index int,@num intse
- Pandas是Python中非常常用的数据处理工具,使用起来非常方便。它建立在NumPy数组结构之上,所以它的很多操作通过NumPy或者Pa
- 本文实例讲述了python获取一组汉字拼音首字母的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/env python
- 首先,说明一下python确实可以根据照片获取地理位置,但是也是有一定的限制条件的。获取照片地理位置的实现思路是这样的:通过提取照片中的经纬
- 一.需求统计收集各个实例上table的信息,主要是表的记录数及大小。收集的范围是cmdb中所有的数据库实例。二.公共基础文件说明1.配置文件
- 女朋友是一个软件测试人员,在工作中经常会遇到需要录屏记录自己操作,方便后续开发同学定位。因为录屏软件动不动就开始收费,所以她经常更换录屏软件