Python爬虫练习:爬取网站动漫图片
前言
有一段没用 python 了,我也不知道自己为什么对 python 越来越淡,可能自己还是比较喜欢 android ,毕竟自己第一次接触编程就是 android,为了android学java,然后接触的python,这次也是因为android,我要用一次python来帮我爬数据
正文
目标网站 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 oshead = "https://divnil.com/wallpaper/iphone8/"
if os.path.exists("./Divnil") != True:os.mkdir("./Divnil")for url in links:url = head + urlresp = requests.get(url, headers=headers)if resp.status_code != requests.codes.OK:print("URL: %s REQUESTS ERROR. CODE: %d" % (url, resp.status_code))continuesoup = 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 osclass 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 + urlresp = requests.get(url, headers=self.headers)if resp.status_code != requests.codes.OK:print("URL: %s REQUESTS ERROR. CODE: %d" % (url, resp.status_code))continuesoup = 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)continueif '/' 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()
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
作者| zckun
来源|简书
Python爬虫练习:爬取网站动漫图片相关推荐
- Python爬虫之爬取绝对领域美女图片
Python爬虫之爬取绝对领域美女图片 第一步: 导入模块: import requests from lxml import etree 第二步:定义函数: def get_url(start_ur ...
- Pycharm + python 爬虫简单爬取网站数据
本文主要介绍简单的写一个爬取网站图片并将图片下载的python爬虫示例. 首先,python爬虫爬取数据,需要先了解工具包requests以及BeautifulSoup requests中文文档:ht ...
- 10-24-程序员日-我的第一个python爬虫项目-爬取蜂鸟上的图片
今天是诸位程序员的节日,为了1024勋章,我这个菜鸟也来发个博客 网络爬虫(又被称为网页蜘蛛,网络机器人)就是模拟浏览器发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序. 在 ...
- python爬虫 多线程 爬取网站排行榜上的所有小说
python爬虫,多线程批量爬取多部小说 欢迎阅读 环境 获取排行榜top上的所有小说地址 分析排行榜top100页面的html,获取所有小说的url 遍历小说列表url到生成txt 多线程启动爬取任 ...
- Python爬虫(1)------爬取网站图片
初学爬虫的学习流程 环境 python 3.6 使用 urlib库进行爬取内容 熟悉爬虫 首先对百度进行爬取 # -*- coding: utf-8 -*- import urllib.request ...
- 【python爬虫】爬取网站数据,整理三句半语料数据集
因为目前没有公开的三句半语料库,所以在网络上爬取一些网站上公开的三句半数据. 主要分为两部分: 目录 爬取数据 清洗数据 爬取数据 以爬取 http://p.onegreen.net/JuBen 上的 ...
- python爬虫之爬取网站小说
继上次的使用类的方法爬取的单页单章小说后,在准备爬取整部小说,遇到点困难,先用函数式编程试试结果. 代码如下: ''' 函数式编程 17K小说网爬取龙井迷案小说 '''# 导入第三方库 import ...
- python爬虫:爬取男生喜欢的图片
任务目标: 1.抓取不同类型的图片 2.编写一个GUI界面爬虫程序,打包成exe重新文件 3.遇到的难点 1.分析如何抓取不同类型的图片 首先打开网站,可以看到有如下6个类型的菜单 在这里插入图片描述 ...
- Python爬虫,爬取百度贴吧图片和视频文件,xpath+lxml,访问被拒的原因分析
目录 百度贴吧图片和视频文件爬取程序 1.需求分析 2.url分析 3.Xpath分析 4.程序设计 5.坑点 百度贴吧图片和视频文件爬取程序 1.需求分析 进入百度贴吧,搜索周杰伦,进入周杰伦吧.我 ...
最新文章
- HTML4.01复习笔记一
- xmind思维导图之如何进行深度工作
- 如何从数学角度解释何恺明新作Masked Autoencoders (MAE)?
- Java IO流读取文件
- flutte的第一个hello world程序
- cuda linux编译器_linux下如何编译CUDA+QT(qtcreator下)
- [社团工作指南]日常工作*组织构架
- Unicode和UTF-8之间的转换详解
- cmd下特殊字符串的处理
- Leetcode: Generate Parentheses
- FLV格式视频转换成MP4格式教程
- Linux安装MySQL5.7
- echart demo
- python图片,大家来找茬
- jmeter显示桌面快捷方式图标
- 例行检查软件列表,突然发现不明sangforvnc应用
- switch范围判断、switch区间判断
- 网络资源下载方式:http/https、ftp/sftp、BT种子、磁力下载、ed2k下载等的区别
- win10 屏幕文字显示不清楚 解决方案
- 《联想本有更好的路走》《杨元庆会不会掉队》《少帅杨元庆》