温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python如何爬取网站动漫图片

发布时间:2021-11-25 14:10:19 来源:亿速云 阅读:203 作者:小新 栏目:大数据

这篇文章将为大家详细讲解有关Python如何爬取网站动漫图片,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

正文

目标网站 https://divnil.com

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

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

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

Python如何爬取网站动漫图片

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

接着分析我们从何入手

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

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

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

Python如何爬取网站动漫图片


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

Python如何爬取网站动漫图片


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

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()

关于“Python如何爬取网站动漫图片”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI