Python爬虫 | 爬取高质量小姐姐照片

发布时间:2022-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Python爬虫 | 爬取高质量小姐姐照片脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Python爬虫 | 爬取高质量小姐姐照片

@H_512_12@
  • 1、数据来分析
  • 2、获取author_id_list和img_id
  • 3、代码实现
    • 3.1、制作detial
    • 3.2、制作detial_list
    • 3.3、数据保存
    • 3.4、批量获取
  • 4、完整代码
  • 声明

  • 1、数据来源分析

      在网页HTML源代码里,我们找到了每一张照片的地址为

    https://photo.tuchong.COM/5489136/f/360962642.jpg

      解析如下

    https://photo.tuchong.com/author_id_list/f/img_id.jpg

      其中author_id_list和img_id都是我们需要自己获取的

    2、获取author_id_list和img_id

      打开网站图虫网首页,经过分析,发现数据请求是动态加载的来源是下面的请求

    Python爬虫 | 爬取高质量小姐姐照片

      返回的数据中包含author_id_list和img_id,一个author_id_list对应好几个img_id,因此在获取数据的时候对每一个author_id_list下的图片进行单独保存,建立独立的文件夹

    Python爬虫 | 爬取高质量小姐姐照片

    3、代码实现

    3.1、制作detial

      将每一个author_id_list和img_id保存到一个detial中,利用键值对的形式,author_id_list存放author_id_list,img_id存放img_id

    Python爬虫 | 爬取高质量小姐姐照片

    3.2、制作detial_list

      将每一个detial存放在detial_list,那么第一页的所需数据就准备好了

    Python爬虫 | 爬取高质量小姐姐照片

    3.3、数据保存

      对每一个author_id建立应的文件夹

    Python爬虫 | 爬取高质量小姐姐照片

    3.4、批量获取

      前面的请求参数有page和count,表示请求页数和每一页的数据量,将page放在循环中就行了

    Python爬虫 | 爬取高质量小姐姐照片

    4、完整代码

    import requests
    import os
    
    if __name__ == '__main__':
        headers = {
            "User-Agent": ";mozilla/5.0 (Windows NT 10.0; Win64; x64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.44",
        }
        for i in range(1, 11):
            url = f'https://tuchong.com/rest/tags/%E7%BE%8E%E5%A5%B3/posts?page={i}&count=20&order=weekly&before_timestamp='
            response = requests.get(url=url, headers=headers)
            page = response.json()["postList"]
    
            # 制作detial_list
            detial_list = []
            for ITem in page:
                author_id_list = ""
                img_id = []
                detial = {
                    "author_id_list": author_id_list,
                    "img_id": img_id
                }
                
                author_id_list = (item["author_id"])
    
                for it in item["images"]:
                    img_id.apPEnd(it["img_id"])
                    
                detial["author_id_list"] = author_id_list
                detial["img_id"] = img_id
                
                detial_list.append(detial)
    
            for item in detial_list:
                # 新建文件夹
                author_id_list = item["author_id_list"]
                if not os.path.exists(f'img/{author_id_list}'):
                    os.makedirs(f"img/{author_id_list}")
                    
                # 保存数据
                for it in item["img_id"]:
                    with open(f'img/{author_id_list}/{it}.jpg', mode="wb") as fp:
                        page = requests.get(url=f"https://photo.tuchong.com/{author_id_list}/f/{it}.jpg", headers=headers)
                        data = page.content
                        fp.write(data)
            # 打印提示信息
            PRint(f"第{i}页完成...")
    
    

      毕竟图虫网提供高质量的图片,而且都是版权所有的,也就只是用于学习交流,不要太过分,爬取10页数据

    Python爬虫 | 爬取高质量小姐姐照片

      源代码地址F1a;gitee

    声明

    本文仅限于做技交流学习,请勿用作任何非法商用!

    脚本宝典总结

    以上是脚本宝典为你收集整理的Python爬虫 | 爬取高质量小姐姐照片全部内容,希望文章能够帮你解决Python爬虫 | 爬取高质量小姐姐照片所遇到的问题。

    如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。