栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

我用Python连夜离线了100G图片,只为了防止网站被消失

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我用Python连夜离线了100G图片,只为了防止网站被消失

20 行代码,变身技术圈多肉小达人

Python 爬虫 120 例,已完成文章清单
  1. 10 行代码集 2000 张美女图,Python 爬虫 120 例,再上征途
  2. 通过 Python 爬虫,发现 60%女装大佬游走在 cosplay 领域
  3. Python 千猫图,简单技术满足你的收集控
  4. 熊孩子说“你没看过奥特曼”,赶紧用 Python 学习一下,没想到
  5. 技术圈的【多肉小达人】,一篇文章你就能做到
用 Python 爬取 100G Cosers 图片 本篇博客目标

爬取目标

  • 目标数据源:http://www.cosplay8.com/pic/chinacos/,又是一个 Cos 网站,该类网站很容易 消失 在互联网中,为了让数据存储下来,我们盘它。

使用的 Python 模块

  • requests,re,os

重点学习内容

  • 今日的重点学习,可放在详情页分页抓取上,该技巧在之前的博客中没有涉及,编写代码过程中重点照顾一下。

列表页与详情页分析

通过开发者工具,可以便捷的分析出目标数据所在的标签。


点击任意图片,进入详情页,得到目标图片为单页展示,即每页展示一张图片。


  

同时获取列表页与详情页 URL 生成规则如下:

列表页

  • http://www.cosplay8.com/pic/chinacos/list_22_1.html
  • http://www.cosplay8.com/pic/chinacos/list_22_2.html
  • http://www.cosplay8.com/pic/chinacos/list_22_3.html

详情页

  • http://www.cosplay8.com/pic/chinacos/2021/0601/61823.html
  • http://www.cosplay8.com/pic/chinacos/2021/0601/61823_2.html
  • http://www.cosplay8.com/pic/chinacos/2021/0601/61823_3.html

注意详情页首页无序号 1,顾爬取获取总页码的同时,需存储首页图片。

编码时间

目标网站对图片进行了分类,即 国内 cos,国外 cos,汉服圈,Lolita,因此在爬取时可以对其进行动态输入,即爬取目标源自定义。

def run(category, start, end):
    # 生成待爬取的列表页
    wait_url = [
        f"http://www.cosplay8.com/pic/chinacos/list_{category}_{i}.html" for i in range(int(start), int(end)+1)]
    print(wait_url)

    url_list = []
    for item in wait_url:
    	# get_list 函数在后文提供
        ret = get_list(item)

        print(f"已经抓取:{len(ret)} 条数据")
        url_list.extend(ret)


if __name__ == "__main__":

    # http://www.cosplay8.com/pic/chinacos/list_22_2.html
    category = input("请输入分类编号:")
    start = input("请输入起始页:")
    end = input("请输入结束页:")
    run(category, start, end)

上述代码首先基于用户的输入,生成目标网址,然后将目标网址一次传递到 get_list 函数中,该函数代码如下:

def get_list(url):
    """
    获取全部详情页链接
    """
    all_list = []

    res = requests.get(url, headers=headers)
    html = res.text
    pattern = re.compile('
  • ') all_list = pattern.findall(html) return all_list
  • 通过正则表达式

  • 匹配列表页中所有详情页地址,并将其进行整体返回。

    在 run 函数中继续增加代码,获取详情页图片素材,并对抓取到的图片进行保存。

    def run(category, start, end):
        # 待爬取的列表页
        wait_url = [
            f"http://www.cosplay8.com/pic/chinacos/list_{category}_{i}.html" for i in range(int(start), int(end)+1)]
        print(wait_url)
    
        url_list = []
        for item in wait_url:
            ret = get_list(item)
    
            print(f"已经抓取:{len(ret)} 条数据")
            url_list.extend(ret)
    
        print(url_list)
        # print(len(url_list))
        for url in url_list:
            get_detail(f"http://www.cosplay8.com{url}")
    

    由于匹配到的详情页地址为相对地址,顾对地址进行格式化操作,生成完整地址。
    get_detail 函数代码如下:

    def get_detail(url):
    	# 请求详情页数据
        res = requests.get(url=url, headers=headers)
        # 设置编码
        res.encoding = "utf-8"
        # 得到网页源码
        html = res.text
    
        # 拆解页码,保存第一张图片
        size_pattern = re.compile('共(d+)页: ')
        # 获取标题,后续发现发表存在差异,顾正则表达式有修改
        # title_pattern = re.compile('(.*?)-Cosplay中国')
        title_pattern = re.compile('(.*?)-Cosplay(中国|8)')
        # 设置图片正则表达式
        first_img_pattern = re.compile(")
        try:
        	# 尝试匹配页码
            page_size = size_pattern.search(html).group(1)
            # 尝试匹配标题
            title = title_pattern.search(html).group(1)
            # 尝试匹配地址
            first_img = first_img_pattern.search(html).group(1)
    
            print(f"URL对应的数据为{page_size}页", title, first_img)
            # 生成路径
            path = f'images/{title}'
            # 路径判断
            if not os.path.exists(path):
                os.makedirs(path)
    
            # 请求第一张图片
            save_img(path, title, first_img, 1)
    
            # 请求更多图片
            urls = [f"{url[0:url.rindex('.')]}_{i}.html" for i in range(2, int(page_size)+1)]
    
            for index, child_url in enumerate(urls):
                try:
                    res = requests.get(url=child_url, headers=headers)
    
                    html = res.text
                    first_img_pattern = re.compile(")
                    first_img = first_img_pattern.search(html).group(1)
    
                    save_img(path, title, first_img, index)
                except Exception as e:
                    print("抓取子页", e)
    
        except Exception as e:
            print(url, e)
    

    上述代码核心逻辑已经编写到注释中,重点在 title 正则匹配部分,初始编写正则表达式如下:

    (.*?)-Cosplay中国
    

    后续发现不能全部匹配成功,修改为如下内容:

    (.*?)-Cosplay(中国|8)
    

    ,缺少的 save_img 函数代码如下:

    def save_img(path, title, first_img, index):
        try:
            # 请求图片
            img_res = requests.get(f"http://www.cosplay8.com{first_img}", headers=headers)
            img_data = img_res.content
    
            with open(f"{path}/{title}_{index}.png", "wb+") as f:
                f.write(img_data)
        except Exception as e:
            print(e)
    

    完整代码下载地址:https://codechina.csdn.net/hihell/python120,No6。

    代码编写过程中,顺手爬取了一堆图片,可以提前预览一波,看看图,在决定是否运行这段代码。

    • 900 M 压缩包
    • 600 M 压缩包
    抽奖时间

    只要评论数过 100
    随机抽取一名幸运读者
    奖励 29.9 元《Python 游戏世界》 1 折购买券一份,只需 2.99 元

    今天是持续写作的第 166 / 200 天。可以点赞、评论、收藏啦。

  • 转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/272566.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号