鱼C论坛

 找回密码
 立即注册
查看: 1772|回复: 4

[作品展示] OOXX网站爬妹子图,80%的内容都是自己考虑的,希望大家看看咋样

[复制链接]
发表于 2019-12-22 17:25:10 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 TJBEST 于 2019-12-22 17:53 编辑

根据小甲鱼推荐的网站设计的下载程序,没有考虑代理所以比较简陋,80%的设计流程是我自己想的没看小甲鱼设计的流程,不过小甲鱼正则表达式文章真是太6了,帮助真大
exe版本我也弄了,传不上去,大家觉得有用可以自己打包。
  1. import urllib.request
  2. import os
  3. import re
  4. class downLoadOOXX():
  5.     def __init__(self):
  6.         self.__page = 0#page当前访问的第几页
  7.         self.__url = 'http://jandan.net/ooxx/MjAxOTEyMjItMQ==#comments'#应该是第一页的页面地址
  8.         self.__nexturl = None #下一页的地址 为下一次下载存储网址
  9.         self.__picurl = []#存储照片地址的列表
  10.         self.__dir = '.\\OOXX'#存储照片的文件夹
  11.         self.__headers = {}#头文件
  12.         self.__headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
  13.         
  14.     def __getResponse(self):#获取html语句
  15.         req = urllib.request.Request(self.__url,headers = self.__headers)
  16.         response = urllib.request.urlopen(req)
  17.         self.__page += 1#获取的页数增加
  18.         html = response.read().decode('utf-8')
  19.         return html
  20.     def __anaylist(self,html):#分析语句
  21.         #利用正则表达式 获取图片和下一页的网址
  22.         #找到下一页的位置并赋值
  23.         nextUrlPattern = re.compile(r'<a title="Newer Comments" href="(//jandan.net/ooxx/\w+==#comments)" class="next-comment-page">上一页</a>')
  24.         matchResult = nextUrlPattern.search(html)
  25.         self.__nexturl = 'http:' + matchResult.group(1)
  26.         #或得图片的网址:
  27.         picUrlPattern = re.compile(r'<span class="righttext"><a href="/t/\d+">\d+</a></span><p><a href="(//\w+\.sinaimg\.cn/\w+/\w+\.jpg)" target="_blank" class="view_img_link" referrerPolicy="no-referrer">')
  28.         matchResult = picUrlPattern.finditer(html)
  29.         for eachPic in matchResult:
  30.             self.__picurl.append('http:' + eachPic.group(1))
  31.     def __downLoad(self):#下载图片
  32.         try:
  33.             os.mkdir(self.__dir)#创建文件夹:
  34.         except Exception as reason:
  35.             pass
  36.         #下载图片
  37.         try:
  38.             os.mkdir(self.__dir + '\\' + str(self.__page))#创建子文件夹:
  39.         except Exception as reason:
  40.             pass
  41.         index = 1
  42.         for eachPic in self.__picurl:
  43.             req = urllib.request.Request(eachPic,headers = self.__headers)
  44.             response = urllib.request.urlopen(req)
  45.             picName = self.__dir + '\\' + str(self.__page) + '\\' + str(self.__page) + '_' + str(index)+ '.jpg'
  46.             with open(picName,'wb') as f:
  47.                 f.write(response.read())
  48.             index += 1
  49.     def action(self,number):#number是一次下载几页 具体的动作页
  50.         index = 0
  51.         while index<number:
  52.             html = self.__getResponse()#获取html语句 string
  53.             self.__anaylist(html) #分析语句 完成self.__picurl的填写 和 self.__nexturl的记录
  54.             self.__downLoad()#下载图片
  55.             self.__url = self.__nexturl
  56.             self.__picurl = []
  57.             index = index + 1
  58. print('嘿嘿,这是下载美图的爬虫哦')
  59. test = downLoadOOXX()
  60. try:
  61.     number = int(input('请输入下载页数'))
  62.     test.action(number)
  63. except ValueError as reason:
  64.     print('呀出错了,请您输入整数')
  65. else:
  66.     print('下载成功!')
  67. while True:
  68.     status = input('是否继续下载:(y/n)y是,n否:')
  69.     if status == 'y':
  70.         try:
  71.             number = int(input('请输入下载页数'))
  72.             test.action(number)
  73.         except ValueError as reason:
  74.             print('呀出错了,请您输入整数')
  75.         else:
  76.             print('下载成功!')
  77.     elif status == 'n':
  78.         break
  79.     else:
  80.         print('请正确选择是否继续下载')
复制代码

评分

参与人数 2荣誉 +6 鱼币 +6 贡献 +3 收起 理由
非常灰太狼 + 3 + 3 + 3 鱼C有你更精彩^_^
zltzlt + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-27 17:34:24 | 显示全部楼层
厉害厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 20:27:16 | 显示全部楼层
有特意回复一下子,,奥利给!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-28 18:07:40 | 显示全部楼层
第9 页以后  url 变了,你的正则表达式要改,不然爬不了 !nextUrlPattern = re.compile(r'<a title="Newer Comments" href="(//jandan.net/ooxx/\w+==#comments)" class="next-comment-page">上一页</a>') 索性 nextUrlPattern = re.compile(r'<a title="Newer Comments" href="(//jandan.net/ooxx/.+=#comments)" class="next-comment-page">上一页</a>'), 就ok了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-13 11:35:34 | 显示全部楼层
你是第一个程序可用的!!赞一个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-13 22:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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