鱼C论坛

 找回密码
 立即注册
查看: 2231|回复: 0

[作品展示] 自己做的一个爬取煎蛋网海量女装照片,可选择本地保存地址,注释详细

[复制链接]
发表于 2021-12-29 09:33:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Prusuit 于 2021-12-29 11:58 编辑
  1. import urllib.request
  2. import chardet
  3. import os
  4. import random
  5. import easygui


  6. def build_opener():
  7.     #  这些IP都是我在网上搜的,检查的时候发现许多都用不了,所以在主函数上把这个函数注释掉了,大家有好的IP可以自己切换
  8.     ip_list1 = ['210.26.124.143:808', '106.75.226.36:808', '125.62.26.197:3128', '120.92.74.189:3128',
  9.                 '113.200.56.13:8010']
  10.     proxy_support = urllib.request.ProxyHandler({'https': random.choice(ip_list1)})
  11.     opener = urllib.request.build_opener(proxy_support)
  12.     urllib.request.install_opener(opener)


  13. def find_picture(html):
  14.     """此函数用来找到网页中所有照片的网站,并返回一个携带有所有网址的代码"""
  15.     picture_list = []
  16.     a = 0
  17.     b = 0
  18.     while b != -1:

  19.         a = html.find('img src=', a) + 9
  20.         if a == 8:
  21.             break
  22.         b = html.find('"', a)
  23.         picture_list.append(f' http:{html[a:b]}')
  24.         a = b + 100

  25.     return picture_list


  26. def find_next_web(html, number):
  27.     """此函数返回一个含有已经解码过的网页的列表"""
  28.     web_list = []
  29.     while number > 1:
  30.         a = html.find("current-comment-page") + 43
  31.         b = html.find(""", a)
  32.         the_url = "http:" + html[a:b]

  33.         req = urllib.request.Request(the_url)
  34.         html = urllib.request.urlopen(req).read()
  35.         encode = chardet.detect(html)['encoding']
  36.         html = html.decode(encode)

  37.         web_list.append(html)

  38.         # 设立循环限制
  39.         number -= 1

  40.     return web_list


  41. def save_picture(picture_list):
  42.     """此函数接收一个含有照片网站的列表,将所有照片保存在指定位置"""
  43.     #  选择所要保存图片的位置
  44.     target = easygui.diropenbox("请指定您要保存的地址")

  45.     # 将工作目录切换到指定位置上,并创立一个文件夹,用来存放照片
  46.     os.chdir(target)
  47.     os.mkdir('girls_pictures')
  48.     os.chdir('girls_pictures')
  49.     n = 0
  50.     for each in picture_list:
  51.         response = urllib.request.urlopen(each)
  52.         picture = response.read()
  53.         with open(f'girl_{n}.jpg', 'wb') as f:
  54.             f.write(picture)
  55.             n += 1


  56. def download_pictures(url):
  57.     """主程序,选择需要下载多少页,并选择图片保存位置"""
  58.     download_page = int(easygui.enterbox("请问您需要下载多少页面的图片"))
  59.     picture_list = []

  60.     # 建立虚拟ip,防止同一个IP过于频繁访问被封
  61.     # build_opener()

  62.     #  添加请求头中的“User-Agent"来模拟浏览器访问
  63.     headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
  64.                              ' AppleWebKit/537.36 (KHTML, like Gecko) '
  65.                              'Chrome/96.0.4664.110 Safari/537.36'}

  66.     # 访问最外层网站,再将其解码
  67.     req = urllib.request.Request(url, headers=headers)
  68.     html = urllib.request.urlopen(req).read()
  69.     encode = chardet.detect(html)['encoding']
  70.     html = html.decode(encode)

  71.     # 通过find_picture()函数找到其中照片地址存放到一个列表中
  72.     for each1 in find_picture(html):
  73.         picture_list.append(each1)

  74.     # 循环打开下一层页面, 并将所有图片地址都存放到picture_list列表中
  75.     for each_html in find_next_web(html, download_page):

  76.         for each in find_picture(each_html):
  77.             picture_list.append(each)

  78.     # 将已经收集到的照片地址一个一个打开并存放指定文件夹中
  79.     save_picture(picture_list)


  80. if __name__ == "__main__":
  81.     url = 'https://jandan.net/girl/'
  82.     download_pictures(url)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 17:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表