python抓取网页图片并放到指定文件夹


Posted in Python onApril 24, 2014

python抓取网站图片并放到指定文件夹

# -*- coding=utf-8 -*-
import urllib2
import urllib
import socket
import os
import re
def Docment():
    print u'把文件存在E:\Python\图(请输入数字或字母)'
    h=raw_input()
    path=u'E:\Python\图'+str(h)
    if not os.path.exists(path):
        os.makedirs(path)
    return path
def getallurl(html):
    reg=r"a href='(.*?\.htm)'"
    allurl= re.compile(reg)
    allList = re.findall(allurl,html)
    return allList
def getHTML(url):
    url=url
    req_header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
    req_timeout = 20
    html='cuowu'
    try:
        req = urllib2.Request(url,None,req_header)
        resp = urllib2.urlopen(req,None,req_timeout)
        html = resp.read()
    except urllib2.URLError as e:
        print e.message
    except socket.timeout as e:
        getHTML(url,fu)
    return html
def getImg(html,path):
    reg = r'img class=IMG_show border=0 src=(.*?\.jpg)'
    imgre= re.compile(reg)
    imgList = re.findall(imgre, html)
    if imgList:
        print 'ghasghg',path
        for imgurl in imgList:
            print imgurl
            content2=urllib2.urlopen(imgurl).read()
            with open(path+'/'+imgurl[-7:],'wb') as code:
                code.write(content2)
    else:
        return 0
def getallurl(html):
    reg=r"a href='(.*?\.htm)'"
    allurl= re.compile(reg)
    allList = re.findall(allurl,html)
    return allList
j=1
i=0
print u'请输入网址:'
ul=raw_input()
print u'开始下载'
print u'第'+str(j)+u'页'
html=getHTML(ul)
allList=getallurl(html)
path=Docment()
getImg(html,path)
while i<len(allList):
    for lis in allList:
        l=lis[i]
        url=r'http://www.umei.cc/p/gaoqing/rihan/'+lis
        i=i+1
        j=j+1
        html=getHTML(url)
        getImg(html,path)
        print u'第'+str(j)+u'页'
    else:
        print u'下载完毕'
Python 相关文章推荐
彻底搞懂Python字符编码
Jan 23 Python
详解Django+Uwsgi+Nginx的生产环境部署
Jun 25 Python
OpenCV+python手势识别框架和实例讲解
Aug 03 Python
python 文件查找及内容匹配方法
Oct 25 Python
Python函数装饰器实现方法详解
Dec 22 Python
Python实现定期检查源目录与备份目录的差异并进行备份功能示例
Feb 27 Python
eclipse创建python项目步骤详解
May 10 Python
使用TensorFlow实现简单线性回归模型
Jul 19 Python
Pandas分组与排序的实现
Jul 23 Python
python gdal安装与简单使用
Aug 01 Python
python手机号前7位归属地爬虫代码实例
Mar 31 Python
Python定时任务APScheduler原理及实例解析
May 30 Python
python字符串替换示例
Apr 24 #Python
python自动安装pip
Apr 24 #Python
python网络编程示例(客户端与服务端)
Apr 24 #Python
python使用PyFetion来发送短信的例子
Apr 22 #Python
Python BeautifulSoup中文乱码问题的2种解决方法
Apr 22 #Python
python中使用smtplib和email模块发送邮件实例
Apr 22 #Python
python用字典统计单词或汉字词个数示例
Apr 22 #Python
You might like
php代码书写习惯优化小结
2013/06/20 PHP
PHP中绘制图像的一些函数总结
2014/11/19 PHP
PHP设计模式之抽象工厂模式实例分析
2019/03/25 PHP
Yii框架自定义数据库操作组件示例
2019/11/11 PHP
php中用unset销毁变量并释放内存
2020/05/10 PHP
BOOM vs RR BO5 第三场 2.14
2021/03/10 DOTA
一些常用的JS功能函数(2009-06-04更新)
2009/06/04 Javascript
在Javascript里访问SharePoint列表数据的实现方法
2011/05/22 Javascript
jquery $.each()使用探讨
2013/09/23 Javascript
jquery内置验证(validate)使用方法示例(表单验证)
2013/12/04 Javascript
轻松创建nodejs服务器(9):实现非阻塞操作
2014/12/18 NodeJs
jquery实现图片上传之前预览的方法
2015/07/11 Javascript
用户代理字符串userAgent可实现的四个识别
2015/09/20 Javascript
详解微信小程序开发—你期待的分享功能来了,微信小程序序新增5大功能
2016/12/23 Javascript
详解百度百科目录导航树小插件
2017/01/08 Javascript
Vue组件中的data必须是一个function的原因浅析
2018/09/03 Javascript
vue interceptor 使用教程实例详解
2018/09/13 Javascript
vue 解决遍历对象显示的顺序不对问题
2019/11/07 Javascript
微信小程序修改数组长度的问题的解决
2019/12/17 Javascript
OpenLayers加载缩放控件使用方法详解
2020/09/25 Javascript
在vue中嵌入外部网站的实现
2020/11/13 Javascript
Python开发如何在ubuntu 15.10 上配置vim
2016/01/25 Python
python list是否包含另一个list所有元素的实例
2018/05/04 Python
Python中时间datetime的处理与转换用法总结
2019/02/18 Python
选择python进行数据分析的理由和优势
2019/06/25 Python
python腾讯语音合成实现过程解析
2019/08/01 Python
pandas 空数据处理方法详解
2019/11/02 Python
工程师必须了解的LRU缓存淘汰算法以及python实现过程
2020/10/15 Python
Opencv常见图像格式Data Type及代码实例
2020/11/02 Python
python中pivot()函数基础知识点
2021/01/03 Python
假日旅行社实习自我鉴定
2013/09/24 职场文书
办公室经理岗位职责
2014/01/01 职场文书
十八大感想感言
2014/02/10 职场文书
运动会横幅标语
2014/06/17 职场文书
2016年教师节特级教师获奖感言
2015/12/09 职场文书
SpringMVC 整合SSM框架详解
2021/08/30 Java/Android