欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-04-07 20:42:30
python抓取网站图片并放到指定文件夹 复制代码 代码如下:# -*- coding=utf-8 -*-import urllib2import urllibimpor...

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'下载完毕'