找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 15341|回复: 10

[代码与实例] 爬虫 爬ooxx图 嘿嘿

7

主题

32

帖子

32

积分

贫民

积分
32
QQ
小鱼 发表于 2017-1-21 14:31:28 | 显示全部楼层 |阅读模式
闲话不多说  直接撸代码

  1. import urllib.request
  2. import urllib.error
  3. import os
  4. import sys
  5. import http.server
  6. import http.client
  7. import time
  8. import re
  9. import random
  10. import math

  11. data = None
  12. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'}
  13. enctype = 'utf-8'
  14. proxies = []
  15. max_error_times = 5        #最多允许失败5次,否则放弃该图片下载

  16. def create_localhost():
  17.     number = int((math.sqrt(5)-1)/2) * len(proxies)
  18.     for x in range(number):
  19.         proxies.append(None)

  20. def get_result(req_or_url,is_retrieve=False,filename = None):         #flag是否使用retrieve
  21.     error_time = 0
  22.     while True:
  23.         try:
  24.             if error_time == max_error_times:
  25.                 print('失败次数达%d次......放弃操作' % max_error_times)
  26.                 return None
  27.             error_time += 1
  28.             if is_retrieve:
  29.                 return urllib.request.urlretrieve(req_or_url,filename)
  30.             else:
  31.                 return urllib.request.urlopen(req_or_url)
  32.         except urllib.error.URLError as e:
  33.             if hasattr(e,'code'):
  34.                 print(e.code,e.reason)
  35.                 change_proxy()
  36.                 continue
  37.             elif hasattr(e,'reason'):
  38.                 print(e)
  39.                 change_proxy()
  40.                 continue
  41.         except (ConnectionResetError,http.client.BadStatusLine) as e:
  42.             print(e)
  43.             change_proxy()
  44.             continue
  45.         except TimeoutError as e:
  46.             print(e)
  47.             print('服务器长时间无响应,自动切换代理.....')
  48.             change_proxy()
  49.             continue

  50. def get_proxy():
  51.     global data,headers,proxies
  52.     req = urllib.request.Request('http://www.xici.net.co',None,headers)
  53.     response = get_result(req)
  54.     html = response.read().decode('utf-8')
  55.     p = re.compile(r'''<tr\sclass[^>]*>\s+
  56.                                     <td>.+</td>\s+
  57.                                     <td>(.*)?</td>\s+
  58.                                     <td>(.*)?</td>\s+
  59.                                     <td>(.*)?</td>\s+
  60.                                     <td>(.*)?</td>\s+
  61.                                     <td>(.*)?</td>\s+
  62.                                     <td>(.*)?</td>\s+
  63.                                 </tr>''',re.VERBOSE)
  64.     proxy_list = p.findall(html)
  65.     for each_proxy in proxy_list[1:]:
  66.         if each_proxy[4] == 'HTTP':
  67.             proxies.append(each_proxy[0]+':'+each_proxy[1])

  68. def change_proxy():
  69.     proxy = random.choice(proxies)
  70.     if proxy == None:
  71.         proxy_support = proxy_support = urllib.request.ProxyHandler({})
  72.     else:
  73.         proxy_support = urllib.request.ProxyHandler({'http':proxy})
  74.     opener = urllib.request.build_opener(proxy_support)
  75.     opener.addheaders = [('User-Agent',headers['User-Agent'])]
  76.     urllib.request.install_opener(opener)
  77.     print('智能切换代理:%s' % ('本机' if proxy==None else proxy))

  78. def get_page():         #获取最大页数
  79.     home = 'http://jandan.net/ooxx'
  80.     global data,headers,enctype
  81.     req = urllib.request.Request(home,data,headers)
  82.     response = get_result(req)
  83.     html = response.read().decode(enctype)
  84.     find_string = 'current-comment-page'
  85.     find_start = html.index(find_string) + len(find_string) + 3
  86.     find_end = html.index(']',find_start+1)
  87.     return int(html[find_start:find_end])
  88. test = None
  89. def get_pic(page):      #生成器,返回一个图片链接
  90.     global data,headers,enctype
  91.     while True:
  92.         url = 'http://jandan.net/ooxx/page-%d' % page
  93.         print('当前页面:%d' % page)
  94.         req = urllib.request.Request(url,data,headers)
  95.         response = get_result(req)
  96.         if response == None:
  97.             print('获取页面失败.....')
  98.             sys.exit()
  99.         html = response.read().decode(enctype)
  100.         pic = re.compile(r'<img\s+src="(http://.+?\.(?:jpg|jpeg|gif))"')
  101.         for pic in pic.finditer(html):
  102.             yield pic.group(1)
  103.         time.sleep(5)
  104.         page -= 1
  105.         if page<1:
  106.             break

  107. save_path = 'D:\\图片\\妹子图'

  108. def download():
  109.     count = 1
  110.     global data,headers
  111.     for pic_url in get_pic(get_page()):         #get_page()改为页数如1000可从1000页开始下载
  112.         file_name = os.path.split(pic_url)[1]
  113.         if not os.path.isdir(save_path):    #目录不存在就创建
  114.             os.makedirs(save_path)
  115.         get_result(pic_url,True,save_path+'\\'+file_name)
  116.         print('本次成功下载第%d个图片! %s' % (count , pic_url))
  117.         count += 1

  118. if __name__ == '__main__':
  119.     get_proxy()
  120.     create_localhost()
  121.     download()

复制代码
回复

使用道具 举报

7

主题

32

帖子

32

积分

贫民

积分
32
QQ
小鱼  楼主| 发表于 2017-1-21 14:32:20 | 显示全部楼层
确切说是妹子图
回复 支持 反对

使用道具 举报

0

主题

3

帖子

3

积分

贫民

积分
3
yyshizhu 发表于 2017-2-5 19:00:44 | 显示全部楼层
不错啊    1024 不知道能不能用
回复 支持 反对

使用道具 举报

0

主题

3

帖子

3

积分

贫民

积分
3
yyshizhu 发表于 2017-2-26 17:25:57 | 显示全部楼层
1024  现在的图都带水印的   下载下来也就那么回事  只能自己看
回复 支持 反对

使用道具 举报

1

主题

2

帖子

2

积分

贫民

积分
2
刘剑锋 发表于 2017-3-25 11:28:44 | 显示全部楼层
有错误
回复

使用道具 举报

0

主题

3

帖子

3

积分

贫民

积分
3
lsle 发表于 2017-3-29 20:43:17 | 显示全部楼层
这个是python3的吧
回复 支持 反对

使用道具 举报

0

主题

1

帖子

1

积分

贫民

积分
1
为你心跳 发表于 2018-4-10 15:52:16 | 显示全部楼层
<p><img src="//img.jandan.net/img/blank.gif" onload="jandan_load_img(this)" /><span class="img-hash">2d930yhbY6YXJ9lDRVX2iNdSOVIJ7a6mT+HlzrGyezKGj9ynLN0fnwlkIQ8ucsrjovOmZngkUtAne+/VApZrWiqHwiWGLO5V9HWEfR1oK/u7W0eFI3ObVw</span></p>

人家网站改了现在需要根据img-hash计算出正真的图片地址了,不知道怎么解啊
回复 支持 反对

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
chenzq2001 发表于 2018-8-8 15:51:30 | 显示全部楼层
直接这个网站  
http://www.mzitu.com
回复 支持 反对

使用道具 举报

0

主题

1

帖子

1

积分

贫民

积分
1
malakashi 发表于 2018-8-24 14:47:13 | 显示全部楼层
真的很厉害。
回复 支持 反对

使用道具 举报

0

主题

5

帖子

5

积分

贫民

积分
5
xixi168 发表于 2019-4-20 23:19:01 | 显示全部楼层
运行没有报错,但在设定的图片存储位置没有发现图片
回复 支持 反对

使用道具 举报

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

本版积分规则

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