精易论坛

标题: [python] 第一个爬虫, 爬妹子写真集图片 [打印本页]

作者: 剑齿虎233    时间: 2017-7-18 23:23
标题: [python] 第一个爬虫, 爬妹子写真集图片
效果图
版本以及需要用到的第三方库代码
  1. #!/usr/bin/env python
  2. #coding:UTF-8

  3. #脚本名称: 爬妹纸写真图
  4. #脚本作者: 剑齿虎
  5. #脚本版本: python2.7
  6. #第三方库: BeautifulSoup

  7. import urllib2
  8. import os
  9. from bs4 import BeautifulSoup
  10. import sys
  11. reload(sys)
  12. sys.setdefaultencoding('gbk') #使用GBK编码

  13. #函数: 进入下载写真集
  14. def DownloadMz(url):
  15.     html    =   urllib2.urlopen(url).read()
  16.     dom_    =   BeautifulSoup(html, 'html.parser') #解析为类似dom的对象

  17.     title   =   dom_.body.select(".main-title")[0].string #找元素:标题
  18.     count   =   dom_.body.select(".pagenavi")[0].findAll('a')[4].span.string #找元素:页数

  19.     #打印信息
  20.     print('title:' + title)
  21.     print('count:' + count)
  22.     print('url:' + url + "\n------------------")

  23.     #保存图片
  24.     for i in range(1,int(count)):
  25.         get_url     =   url + "/" + str(i) #分页地址
  26.         save_path   =   os.getcwd() + "\\Download\" + str(title)  #保存目录

  27.         #创建保存目录, 如果不存在
  28.         if os.path.exists(save_path)==False:
  29.             os.mkdir(save_path)

  30.         #保存图片
  31.         save_path   =   save_path + "\" + str(i) + ".jpg" #保存文件名
  32.         SaveImage(get_url, save_path)
  33.         print("-- OK: "+ save_path)

  34.     print("---------- END -----------\n\n")


  35. #函数: 保存每页图片
  36. def SaveImage(get_url, savePath):
  37.     #获取图片地址
  38.     html    =   urllib2.urlopen(get_url).read()
  39.     dom_    =   BeautifulSoup(html, 'html.parser')
  40.     src     =   dom_.select(".main-image")[0].p.a.img['src']

  41.     #保存呀保存
  42.     f       =   open(savePath, 'wb')
  43.     f.write(urllib2.urlopen(src).read())
  44.     f.close()







  45. #来啊~ 快活呀
  46. url     =   "http://www.mzitu.com/japan/"
  47. html    =   urllib2.urlopen(url).read()
  48. bs      =   BeautifulSoup(html, 'html.parser')
  49. pages   =   bs.body.select(".page-numbers")[5]['href'] #取总页数html

  50. #取爬取页面分页总数
  51. cur_end     =   pages.rindex("/", 0)
  52. cur_start   =   pages.rindex("/", 0, cur_end) + 1
  53. page_count  =   pages[cur_start:cur_end] #页数

  54. for i in range(1, int(page_count)):
  55.     page_url    =   url + "/page/" + str(i) #分页url
  56.     print("*** PAGE *** " + str(i) + "/" + str(page_count) + " $ " + page_url)

  57.     dom_        =   BeautifulSoup(urllib2.urlopen(page_url).read(), 'html.parser') #取分页dom
  58.     uls     =   bs.body.findAll('ul',{'id':'pins'})[0].findAll("li") #获取写真集列表

  59.     #进入下载写真集
  60.     for li in uls:
  61.         DownloadMz(str(li.span.a['href']))
复制代码


目前正在学习Python, 为了更能快速学到知识. 最好就是实践一个小项目, 一般看文档一般搜百度一边写程序, 虽说踩的坑不少, 但也积累了经验.
这个程序还存在个问题, 就是”写真集”没有分页, 下载完第一页就结束了. 有兴趣的小伙伴可以改改
“脚本目录需要新建一个 Download 目录, 脚本不会自己创建这个目录的~”



作者: eoo    时间: 2017-7-19 01:56
......................
作者: 雪山凌狐    时间: 2017-7-19 04:27
可以,第一个爬虫就那么迫不及待哈哈~支持~
作者: SalHe    时间: 2017-7-19 08:11
最近也开始入坑Python  给楼主点个赞
作者: `Xk    时间: 2017-7-19 12:41
火钳刘明
作者: yuanqun    时间: 2017-8-10 12:29
python2.7与python3.6差别太大,用3.6无法运行,thml通不过。




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4