50 行 Python 代码抓取 divnil 动漫妹子图!

2019 年 4 月 26 日 机器学习算法与Python学习


源 / 简书 & 小象       文 / zckun

源码下载:请点击阅读原文

目标网站 https://divnil.com

  • 首先看看这网站是怎样加载数据的;
    打开网站后发现底部有下一页的按钮,ok,爬这个网站就很简单了;


  • 我们目标是获取每张图片的高清的源地址,并且下载图片到桌面;
    先随便打开一张图片看看详细;
    emmm,只有一张图

看起来还挺清晰的,单击新窗口打开图片

然后下载图片,说实话,这图片很小,我很担心不是高清原图(管他的);

PS:一定要禁用广告拦截插件,不然加载不出图,我就在这被坑T_T;

接着分析我们从何入手

1、先去主页面获取每个图片的详细页面的链接

这链接还是比较好获取的,直接 F12 审核元素,或者右键查看代码,手机上chrome和firefox在url前面加上 "view-source"

比如:view-source:https://www.baidu.com/

2、从详细页面获取图片大图地址
随便打开一个图片详细页面如图:

接着按 F12 审核元素,我们需要定位该图片的链接,首先单击左上角的这玩意儿,像一个鼠标的图标:

接着只需要单击网页上的图片就能定位到代码了:

3、用大图地址下载该图片

这个很简单,看代码

  • 先安装 Requests 和 BeautifulSoup 库

pip install requests bs4

  • 导入库

import requestsfrom bs4 import BeautifulSoupimport sys

  • 请求获取网页源代码

url = "https://divnil.com/wallpaper/iphone8/%E3%82%A2%E3%83%8B%E3%83%A1%E3%81%AE%E5%A3%81%E7%B4%99_2.html"
headers = {
"User-Agent""Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0",
}
resp = requests.get(url, headers=headers)
if resp.status_code != requests.codes.OK:
print("Request Error, Code: %d"% resp.status_code)
sys.exit()

  • 然后解析出所有图片的详细地址

soup = BeautifulSoup(resp.text, "html.parser")
contents = soup.findAll("div", id="contents")[0]
wallpapers = contents.findAll("a", rel="wallpaper")
links = []
for wallpaper in wallpapers:
 links.append(wallpaper[ href ])

  • 接着在详细网页里获取那个看似高清的图片的不确定是否为真实图片链接并下载(/滑稽)

import os

head = "https://divnil.com/wallpaper/iphone8/"
if os.path.exists("./Divnil") != True:
 os.mkdir("./Divnil")

for url in links:
 url = head + url
 resp = requests.get(url, headers=headers)
 if  resp.status_code != requests.codes.OK:
   print("URL: %s REQUESTS ERROR. CODE: %d" % (url, resp.status_code))
   continue
 soup = BeautifulSoup(resp.text, "html.parser")
 img =  soup.find("div", id="contents").contents.find("img", id="main_content")
 img_url = head + img[ "original ].replace("../""")
 img_name = img[ alt ]
 print("start download %s ..." % img_url)

 resp = requests.get(img_url, headers=headers)
 if resp.status_code != requests.codes.OK:
   print("IMAGE %s DOWNLOAD FAILED." % img_name)

 with open("./Divnil/" + img_name + ".jpg""wb"as f:
   f.write(resp.content)

  • 主要代码

  • 完成,贴上所有代码

import requests
from bs4 import BeautifulSoup
import sys
import os


class Divnil:

   def __init__(self):
       self.url = "https://divnil.com/wallpaper/iphone8/%E3%82%A2%E3%83%8B%E3%83%A1%E3%81%AE%E5%A3%81%E7%B4%99.html"
       self.head = "https://divnil.com/wallpaper/iphone8/"
       self.headers = {
           "User-Agent""Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0",
       }
   

   def getImageInfoUrl(self):

       resp = requests.get(self.url, headers=self.headers)
       if resp.status_code != requests.codes.OK:
           print("Request Error, Code: %d"% resp.status_code)
           sys.exit()

       soup = BeautifulSoup(resp.text, "html.parser")
       
       contents = soup.find("div", id="contents")
       wallpapers = contents.findAll("a", rel="wallpaper")
       
       self.links = []
       for wallpaper in wallpapers:
           self.links.append(wallpaper[ href ])

   
   def downloadImage(self):

       if os.path.exists("./Divnil") != True:
           os.mkdir("./Divnil")

       for url in self.links:
           
           url = self.head + url
           
           resp = requests.get(url, headers=self.headers)
           if  resp.status_code != requests.codes.OK:
               print("URL: %s REQUESTS ERROR. CODE: %d" % (url, resp.status_code))
               continue
           
           soup = BeautifulSoup(resp.text, "html.parser")
           
           img = soup.find("div", id="contents").find("img", id="main_content")
           img_url = self.head + img[ original ].replace("../""")
           img_name = img[ alt ]
           
           print("start download %s ..." % img_url)

           resp = requests.get(img_url, headers=self.headers)
           if resp.status_code != requests.codes.OK:
               print("IMAGE %s DOWNLOAD FAILED." % img_name)
               continue

           if  /  in img_name:
               img_name = img_name.split( / )[1]

           with open("./Divnil/" + img_name + ".jpg""wb"as f:
               f.write(resp.content)


   def main(self):
       self.getImageInfoUrl()
       self.downloadImage()


if __name__ == "__main__":
   divnil = Divnil()
   divnil.main()

完整代码地址:https://github.com/ZCkun/divnilCrawler

推荐阅读

还没毕业,我的专业就被社会淘汰了

看我Git 72变,GitHub发布4已超过8000星

5 个Python高级应用,你确定知道?

11行Python代码,盗取了室友的U盘内容

喜欢就点击“在看”吧!
登录查看更多
0

相关内容

【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
195+阅读 · 2020年6月29日
干净的数据:数据清洗入门与实践,204页pdf
专知会员服务
162+阅读 · 2020年5月14日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
118+阅读 · 2020年5月10日
【资源】100+本免费数据科学书
专知会员服务
108+阅读 · 2020年3月17日
《代码整洁之道》:5大基本要点
专知会员服务
50+阅读 · 2020年3月3日
【电子书】Flutter实战305页PDF免费下载
专知会员服务
23+阅读 · 2019年11月7日
Keras作者François Chollet推荐的开源图像搜索引擎项目Sis
专知会员服务
30+阅读 · 2019年10月17日
机器学习入门的经验与建议
专知会员服务
94+阅读 · 2019年10月10日
CVE-2018-7600 - Drupal 7.x 远程代码执行exp
黑客工具箱
14+阅读 · 2018年4月17日
Python 爬虫实践:《战狼2》豆瓣影评分析
数据库开发
5+阅读 · 2018年3月19日
Python | 50行代码实现人脸检测
计算机与网络安全
3+阅读 · 2018年1月23日
教你用Python来玩跳一跳
七月在线实验室
6+阅读 · 2018年1月2日
教你用Python爬虫股票评论,简单分析股民用户情绪
数据派THU
10+阅读 · 2017年12月12日
Python NLP入门教程
Python开发者
9+阅读 · 2017年11月19日
Python NLP 入门教程
大数据技术
19+阅读 · 2017年10月24日
Python3爬虫之入门和正则表达式
全球人工智能
7+阅读 · 2017年10月9日
[编程经验] CVPR2017论文全集下载代码脚本分享
机器学习和数学
9+阅读 · 2017年7月27日
代码这样写不止于优雅(Python版)
数说工作室
4+阅读 · 2017年7月17日
Arxiv
14+阅读 · 2019年11月26日
Arxiv
35+阅读 · 2019年11月7日
Knowledge Distillation from Internal Representations
Arxiv
4+阅读 · 2019年10月8日
A Survey on Deep Learning for Named Entity Recognition
Arxiv
73+阅读 · 2018年12月22日
Arxiv
6+阅读 · 2018年3月31日
Arxiv
3+阅读 · 2012年11月20日
VIP会员
相关VIP内容
【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
195+阅读 · 2020年6月29日
干净的数据:数据清洗入门与实践,204页pdf
专知会员服务
162+阅读 · 2020年5月14日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
118+阅读 · 2020年5月10日
【资源】100+本免费数据科学书
专知会员服务
108+阅读 · 2020年3月17日
《代码整洁之道》:5大基本要点
专知会员服务
50+阅读 · 2020年3月3日
【电子书】Flutter实战305页PDF免费下载
专知会员服务
23+阅读 · 2019年11月7日
Keras作者François Chollet推荐的开源图像搜索引擎项目Sis
专知会员服务
30+阅读 · 2019年10月17日
机器学习入门的经验与建议
专知会员服务
94+阅读 · 2019年10月10日
相关资讯
CVE-2018-7600 - Drupal 7.x 远程代码执行exp
黑客工具箱
14+阅读 · 2018年4月17日
Python 爬虫实践:《战狼2》豆瓣影评分析
数据库开发
5+阅读 · 2018年3月19日
Python | 50行代码实现人脸检测
计算机与网络安全
3+阅读 · 2018年1月23日
教你用Python来玩跳一跳
七月在线实验室
6+阅读 · 2018年1月2日
教你用Python爬虫股票评论,简单分析股民用户情绪
数据派THU
10+阅读 · 2017年12月12日
Python NLP入门教程
Python开发者
9+阅读 · 2017年11月19日
Python NLP 入门教程
大数据技术
19+阅读 · 2017年10月24日
Python3爬虫之入门和正则表达式
全球人工智能
7+阅读 · 2017年10月9日
[编程经验] CVPR2017论文全集下载代码脚本分享
机器学习和数学
9+阅读 · 2017年7月27日
代码这样写不止于优雅(Python版)
数说工作室
4+阅读 · 2017年7月17日
Top
微信扫码咨询专知VIP会员