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 相关文章推荐
Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
Dec 04 Python
12步教你理解Python装饰器
Feb 25 Python
深入理解Python变量与常量
Jun 02 Python
浅析Python的web.py框架中url的设定方法
Jul 11 Python
Python编程中NotImplementedError的使用方法
Apr 21 Python
解决pip install的时候报错timed out的问题
Jun 12 Python
详解Numpy中的广播原则/机制
Sep 20 Python
Python3.5面向对象程序设计之类的继承和多态详解
Apr 24 Python
numpy和pandas中数组的合并、拉直和重塑实例
Jun 28 Python
Python的matplotlib绘图如何修改背景颜色的实现
Jul 16 Python
Keras 实现加载预训练模型并冻结网络的层
Jun 15 Python
python re模块和正则表达式
Mar 24 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
成本8450万,票房仅2亿,口碑两极分化,又一部DC电影扑街了
2020/04/09 欧美动漫
PHP中new static()与new self()的区别异同分析
2014/08/22 PHP
PHP速成大法
2015/01/30 PHP
php上传图片并压缩的实现方法
2015/12/22 PHP
tp5(thinkPHP5)框架连接数据库的方法示例
2018/12/24 PHP
PHP如何实现阿里云短信sdk灵活应用在项目中的方法
2019/06/14 PHP
Nigma vs Liquid BO3 第二场2.14
2021/03/10 DOTA
jQuery 源码分析笔记(7) Queue
2011/06/19 Javascript
浅析jquery的作用与优势
2013/12/02 Javascript
JavaScript+html5 canvas制作的圆中圆效果实例
2016/01/27 Javascript
基于javascript实现精确到毫秒的倒计时限时抢购
2016/04/17 Javascript
详解HTTPS 的原理和 NodeJS 的实现
2017/07/04 NodeJs
vue 实现全选全不选的示例代码
2018/03/29 Javascript
如何自动化部署项目?折腾服务器之旅~
2019/04/16 Javascript
node.js基于socket.io快速实现一个实时通讯应用
2019/04/23 Javascript
js中的this的指向问题详解
2019/08/29 Javascript
Cordova(ionic)项目实现双击返回键退出应用
2019/09/17 Javascript
[01:01:13]2018DOTA2亚洲邀请赛 4.5 淘汰赛 Mineski vs VG 第三场
2018/04/06 DOTA
web.py在模板中输出美元符号的方法
2014/08/26 Python
Python的Django REST框架中的序列化及请求和返回
2016/04/11 Python
python实现Decorator模式实例代码
2018/02/09 Python
python 读取DICOM头文件的实例
2018/05/07 Python
Python爬虫包BeautifulSoup学习实例(五)
2018/06/17 Python
python调用摄像头显示图像的实例
2018/08/03 Python
python读取和保存图片5种方法对比
2018/09/12 Python
Django Haystack 全文检索与关键词高亮的实现
2020/02/17 Python
python中openpyxl和xlsxwriter对Excel的操作方法
2021/03/01 Python
对Pytorch 中的contiguous理解说明
2021/03/03 Python
美国牙科折扣计划:DentalPlans.com
2019/08/26 全球购物
大学生求职自我评价
2014/01/16 职场文书
消防安全责任书范本
2014/04/15 职场文书
保护环境建议书100字
2014/05/13 职场文书
企业法人代表证明书
2014/09/27 职场文书
施工安全协议书
2016/03/22 职场文书
Java用自带的Image IO给图片添加水印
2021/06/15 Java/Android
MySQL中TIMESTAMP类型返回日期时间数据中带有T的解决
2022/12/24 MySQL