爬虫实例(naruto)

import urllib.request
import re
import os


def download_naruto():
    # 转到该文件夹下
    os.chdir('NARUTO')

    for i in range(2,5):
        # 某一页的网址
        url = 'http://www.narutom.com/wallpaper/index_'+str(i)+'.html'
        # 请求该页面
        response = urllib.request.urlopen(url)
        # 读取该页面的数据
        data = response.read().decode('gbk')

        # 建立正则表达式
        pattern = re.compile('/d/file/wallpaper/.*?jpg')
        # 匹配正则
        result= pattern.findall(data)


        # 循环列表中的图片
        for i in result:
            # 建立图片的网址
            url_photo = 'http://www.narutom.com'+i
            # 请求图片
            response = urllib.request.urlopen(url_photo)
            # 读取图片
            data_photo = response.read()

            # 获取图片名
            filename = i.split('/')[-1]
            #写入图片
            with open(filename,'wb')as f:
                f.write(data_photo)


download_naruto()

猜你喜欢

转载自blog.csdn.net/qq_42426237/article/details/81606420
今日推荐