Python 爬虫爬取图片(未分页)

#import requests
import urllib
import re


#res = requests.get('https://www.douban.com/')
#res = urllib.urlopen('https://www.douban.com').read()  #web url 1
res = urllib.urlopen('https://www.douban.com/location/wuhan/events/week-all').read()  #web url 2

down_path = r'C:\liujwFiles\NON_IBM_Files\PycharmProjects\pa_chong_files'

# r : current characters will not be transferred meaning
#reg = r'data-origin="(.*?)"'   # key words1
reg = r'data-lazy="(.*?)"'   # key words2
url_list = re.findall(reg, res)

download_count = len(url_list)
print "Begin to download files.........there are %i files in total....." % download_count
for url in url_list:
    try:

        #There are 2 types of image url format:
        #data - origin = "https://img3.doubanio.com/mpic/s29494781.jpg"
        #data-origin="https://img3.doubanio.com/view/ark_article_cover/large/public/35300210.jpg?v=1499854173.0"
        image_name = url.split('/')[-1].split('?')[0]
        urllib.urlretrieve(url, '%s\download_img.%s' % (down_path, image_name))
    except IOError:
        print "File %s download failed......." % url.split('/')[-1]
        download_count = download_count - 1


print "Download complete!  %i files download successfully" % download_count



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值