设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 19409|回复: 3
打印 上一主题 下一主题

抓取美女图片的爬虫小程序

[复制链接]

0

主题

1

好友

38

积分

新手上路

Rank: 1

楼主
发表于 2015-9-7 13:13:52 |显示全部楼层
@crossin先生  我自己也写了一个抓取图片的脚本,但是在下载图片到电脑的时候发生urllib.error.ContentTooShortError: <urlopen error retrieval incomplete: got only 36211 out of 508217 bytes>的错误,再次执行,却没有任何问题,怀疑是不是和网络有关?由于网速较慢,导致下载图片到内存中的部分较少或者内存占用率较高导致?有没有什么办法能够避免这种错误发生?
回复

使用道具 举报

0

主题

1

好友

38

积分

新手上路

Rank: 1

沙发
发表于 2016-4-13 16:25:55 |显示全部楼层
不清楚之前该网站的源代码怎么写的,但是目前该网站很多地方貌似做了改版,没有实际测试lz的代码,估计已经不能用了吧!
回复

使用道具 举报

0

主题

1

好友

38

积分

新手上路

Rank: 1

板凳
发表于 2016-4-13 21:37:36 |显示全部楼层
既然这样,给大家一个最新该网站爬虫:
  1. from bs4 import BeautifulSoup
  2. import urllib.request
  3. from collections import deque,defaultdict
  4. import re
  5. import os


  6. url='http://www.22mm.cc/'

  7. def get_page(url):
  8.     headers = {
  9.     'Connection': 'Keep-Alive',
  10.     'Accept': "image/png,image/*;q=0.8,*/*;q=0.5",
  11.     'Accept-Language': "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
  12.     'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0",
  13.     'Referer':"http://www.22mm.cc/"
  14.     }
  15.     req = urllib.request.Request(url, headers=headers)
  16.     response = urllib.request.urlopen(req)
  17.     responseutf8 = response.read().decode()
  18.     responsegbk = responseutf8.encode('gbk', 'ignore')
  19.     page = responsegbk.decode('gbk')
  20.     return page

  21. def get_first_link(page):
  22.     queue=deque()
  23.     filename=[]
  24.     baseurl='http://22mm.xiuna.com'
  25.     soup=BeautifulSoup(page)
  26.     pic=soup.find(id = 'recshowBox')
  27.     for child in pic.children:
  28.          queue.append(baseurl+child['href'])
  29.          filename.append(child['title'])
  30.     return filename,queue

  31. def get_all_links(filename, queue):
  32.     d=defaultdict(set)
  33.     n=0
  34.     while queue:
  35.         link2=[]
  36.         linkss=link2[:]
  37.         links=link2[:]
  38.         link1=link2[:]
  39.         baselink=queue.popleft()
  40.         pagecode=get_page(baselink)
  41.         soup=BeautifulSoup(pagecode)
  42.         link=soup.find(class_="pagelist")
  43.         for child in link.children:
  44.             try:
  45.                 linkss.append(child['href'])
  46.             except:
  47.                 continue
  48.         links=linkss[1:-1]
  49.         pattern=re.compile('http.*/')
  50.         addurl=re.findall(pattern,baselink)
  51.         link1=[addurl[0]+link for link in links]
  52.         '''for link in links:
  53.             link1.append(addurl[0]+link)
  54.             link1.append(baselink)'''
  55.         for value in link1:
  56.             d[filename[n]].add(value)
  57.         n+=1
  58.     return d

  59. def download_all_pic(d):
  60.     for key in d:
  61.         print("正在创建{}的目录".format(key))
  62.         path='D:\\pics\\'+key+'\\'
  63.         os.mkdir('%s'%path)
  64.         print(path)
  65.         print("开始下载{}的图集...".format(key))
  66.         index=1
  67.         for i in d[key]:
  68.             global index
  69.             page1=get_page(i)
  70.             pattern1=re.compile(r'arrayImg\[0\]="(http.*?jpg)')
  71.             addurl1=re.findall(pattern1,page1)
  72.             down=addurl1[-1].replace('big','pic')
  73.             print("正在下载%s的第%d张图片" % (key,index))
  74.             urllib.request.urlretrieve(down,'D:\\pics\%s\%d.jpg' % (key,index))
  75.             print("下载完成")
  76.             index+=1

  77. def start(url):
  78.     page=get_page(url)
  79.     filename, queue=get_first_link(page)
  80.     d=get_all_links(filename,queue)
  81.     download_all_pic(d)

  82. start(url)
复制代码
采用了bs4完成
回复

使用道具 举报

0

主题

1

好友

38

积分

新手上路

Rank: 1

地板
发表于 2016-4-13 21:46:29 |显示全部楼层
几点说明:
1.该爬虫只是爬取该网站展示区的几组图片,全站的?自己扩展一下,不想要那么多图片,故一开始就没那些写;
2.D:\\pics,代码没有主动判断D盘是否有pics目录,直接下载的,故需要你提前在D盘创建该目录,否则可能无法运行比较懒……;
3.基于py3.4 win7 pycharm4.5测试可用,其他未测试;
4.不论使用哪种编辑器运行,注意设置好encoding(包括代码本身和对网页内容的解码),否则对这种中文网站encode可能会报错;
5.这种爬虫具有时效性,万一网页改版(例如使用JavaScript)就会失效,需要再次修正方可使用;
最终效果:
代码如果执行无误,会在你的D盘pics下创建几个目录,并在目录内下载相应的图片……
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-6 21:10 , Processed in 0.027057 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部