Python实例.1爬取网页的图片

这个网站图片资源比较丰富,而且还比较搞笑,先尝试着爬取这个网站的图片资源

 我把下载图片的代码写在了模块化的py文件里:

  my_down.py

import requests
import os
import re

def down_img(url, path):
    if not url.startswith('http'):
        url = "http:" + url
    print('down_img:%s' % url)
    try:
        with requests.get(url) as r:
            if r.status_code == 200:
                data = r.content
                if data != '':
                    with open(path, 'wb') as f:
                        f.write(data)
                        print('down_img success,path:%s'%path)
                        return path
            else:
                print('download image fail:%d' % r.status_code)
    except requests.exceptions.ConnectionError:
        print('down_img exception:%s' % url)


def down_images(url, folder_path):
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    pattern = r'<img[^>]*?src="([^"]+)"[^>]*?>'
    header = {'user_agent': 'Mozilla/5.0'}
    with requests.get(url, headers=header) as r:
        if r.status_code == 200:
            images = re.findall(pattern, r.text)
            print('images:%s' % images)
            if len(images) == 0:
                return
            position = 1
            for img_url in images:
                filename=str(position) + ".jpg"
                url_array = img_url.split('/')
                if len(url_array) > 0:
                   filename=url_array[-1]
                img_path = folder_path + filename
                down_img(img_url, img_path)
                position += 1
        else:
            print('access url fail:%d' % r.status_code)

调用方式代码:

import my_down

url = 'https://www.haha.mx/pic/new/%d'
path='E:/pic/haha/'
for i in range(10):
  page=i+1
  page_url=url%page
  page_path=path+str(page)+'/'
  my_down.down_images(page_url,page_path)

下载了好多的图片了:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值