Python爬取天堂网图片
运行代码后 看到生成了一个文件夹,储存的有照片
# coding=utf-8
# 上节代码追加需求:同一图集图片放入同一文件夹下。
import requests
from lxml import etree
import os
# 风景分类下
# url_list = []
# for i in range(2, 71):
# url = f'http://www.ivsky.com/tupian/ziranfengguang/index_{i}.html'
# url_list.append()
# 风景分类/分页列表页
resp = requests.get('http://www.ivsky.com/tupian/ziranfengguang/index_2.html')
if resp.status_code == 200:
html_content = resp.content.decode()
print(html_content)
# 获取图解列表页每个图集链接
basedir = os.path.dirname(__file__)
html_content = r"""
<!doctype html><html><head><meta charset=utf-8 /><title>自然风光图片 - 自然风景图片_第2页 (天堂图片网)</title><meta name="description" content="自然风光图片 - 自然风景图片," /><meta name="keywords" content="" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /><meta name="renderer" content="webkit" /><meta name="applicable-device" content="pc" /><link rel="alternate" media="only screen and(max-width: 640px)" href="http://m.ivsky.com/tupian/ziranfengguang/index_2.html"><meta name="mobile-agent" content="format=html5; url=http://m.ivsky.com/tupian/ziranfengguang/index_2.html"><link type="text/css" href="/img/a.css" rel="stylesheet" /><script type="text/javascript" src="/img/jq.js"></script><script type="text/javascript" src="/img/a.js"></script></head><body><div id="header"><div class="box"><div id="logo"><a href="http://www.ivsky.com">天堂图片网</a></div><ul id="menu"><li><a href="/">首页</a></li><li><a href="/tupian/">图片大全</a></li><li><a href="/bizhi/">桌面壁纸</a></li></ul><div id="search"><div class="inp"><input type="text" id="ser_inp" class="ser_inp" /></div><div class="inp-btn"><input type="submit" value="" id="ser_btn" class="ser_btn" /></div></div><div id="login"></div></div><div class="hbg"></div></div><div class="box"><div id="alltop"><script>dy("alltop");</script></div><div id="tplisttop1"><script>dy("tplisttop1");</script></div><div id="tplisttop2"><script>dy("tplisttop2");</script></div></div><div class="box"><div class="pos"><a href='http://www.ivsky.com/'>首页</a> > <a href="/tupian/">图片大全</a> > <a href="/tupian/ziranfengguang/index_2.html">自然风光图片 - 自然风景图片</a> / 第2页</div><div class="left"><div class="sort"><ul class="tpmenu"><li class="s1"><a href="/tupian/" title="图片大全 - 唯美图片 - 好看的图片">所有图片</a></li><li class="s2on active"><a href="/tupian/ziranfengguang/" title="自然风光图片 - 自然风景图片">自然风光</a></li><li class="s3"><a href="/tupian/chengshilvyou/" title="城市旅游图片 - 世界各国城市图片">城市旅游</a></li><li class="s4"><a href="/tupian/dongwutupian/" title="动物图片 - 宠物图片 - 野生动物图片">动物图片</a></li><li class="s5"><a href="/tupian/zhiwuhuahui/" title="植物花卉图片 - 花图片 - 花草图片 - 植物图片">植物花卉</a></li><li class="s6"><a href="/tupian/haiyangshijie/" title="海洋世界 - 海洋风光图片 - 海底世界图片">海洋世界</a></li><li class="s7"><a href="/tupian/renwutupian/" title="人物图片 - 人物图片大全">人物图片</a></li><li class="s8"><a href="/tupian/meishishijie/" title="美食图片 - 国外美食甜点图片 - 中国传统美食小吃图片">美食世界</a></li><li class="s9"><a href="/tupian/wupin/" title="物品物件大全 - 生活用品 - 学习用品图片">物品物件</a></li><li class="s10"><a href="/tupian/yundongtiyu/" title="运动图片 - 体育、休闲、竞赛图片">运动体育</a></li><li class="s11"><a href="/tupian/jiaotongyunshu/" title="公路铁路运输 - 桥梁码头图片 - 车船飞机图片">交通运输</a></li><li class="s12"><a href="/tupian/jianzhuhuanjing/" title="世界著名建筑 - 各国建筑图片 - 建筑环境图片">建筑环境</a></li><li class="s13"><a href="/tupian/jiaju/" title="家居装饰设计图片 - 装修效果图 - 时尚家居图片">装饰装修</a></li><li class="s14"><a href="/tupian/guanggaosheji/" title="品牌广告设计 - 广告设计创意图片">广告设计</a></li><li class="s15"><a href="/tupian/katongtupian/" title="卡通插画图片 - 卡通人物形象、卡通风景图片">卡通图片</a></li><li class="s16"><a href="/tupian/jieritupian/" title="中国传统节日图片 - 国外知名节日图片">节日图片</a></li><li class="s17"><a href="/tupian/shejisucai/" title="设计素材图片 - 背景图片 - 花纹、底纹、边框素材图片">设计素材</a></li><li class="s18"><a href="/tupian/yishu/" title="艺术 - 绘画 - 民族艺术">艺术绘画</a></li><li class="s19"><a href="/tupian/qita/" title="其他类别图片">其他类别</a></li></ul></div><div class="sline" id="sline2"><div><b>小分类</b><a href="/tupian/ziranfengjing_t2800/" title="自然风景图片_自然风景图片大全">自然风景</a> <a href="/tupian/tiankong_t811/" title="天空图片">天空</a> <a href="/tupian/lantianbaiyun_t1485/" title="蓝天白云图片">蓝天白云</a> <a href="/tupian/yangguang_t599/" title="阳光图片">阳光</a> <a href="/tupian/richu_t165/" title="日出图片">日出</a> <a href="/tupian/wanxia_t84/" title="晚霞图片">晚霞</a> <a href="/tupian/xiyang_t15/" title="夕阳图片">夕阳</a> <a href="/tupian/luori_t8846/" title="落日图片">落日</a> <a href="/tupian/xingkong_t810/" title="星空图片">星空</a> <a href="/tupian/yekong_t19444/" title="夜空图片">夜空</a> <a href="/tupian/tudi_t4728/" title="土地图片">土地</a> <a href="/tupian/gebi_t19445/" title="戈壁图片">戈壁</a> <a href="/tupian/shamo_t2726/" title="沙漠图片">沙漠</a> <a href="/tupian/xiagu_t94/" title="峡谷图片">峡谷</a> <a href="/tupian/shanmai_t95/" title="山脉图片">山脉</a> <a href="/tupian/shanchuan_t2213/" title="山川图片">山川</a> <a href="/tupian/shanlin_t19446/" title="山林图片">山林</a> <a href="/tupian/senlin_t2303/" title="森林图片">森林</a> <a href="/tupian/shulin_t3166/" title="树林图片">树林</a> <a href="/tupian/caoyuan_t933/" title="草原图片">草原</a> <a href="/tupian/tianye_t1472/" title="田野图片">田野</a> <a href="/tupian/caodi_t2229/" title="草地图片">草地</a> <a href="/tupian/tianyuan_t3171/" title="田园图片">田园</a> <a href="/tupian/nongtian_t5097/" title="农田图片">农田</a> <a href="/tupian/maitian_t268/" title="麦田图片">麦田</a> <a href="/tupian/caitian_t19447/" title="菜田图片">菜田</a> <a href="/tupian/daotian_t3887/" title="稻田图片">稻田</a> <a href="/tupian/titian_t10931/" title="梯田图片">梯田</a> <a href="/tupian/heliu_t11199/" title="河流图片">河流</a> <a href="/tupian/xiliu_t169/" title="溪流图片">溪流</a> <a href="/tupian/pubu_t163/" title="瀑布图片">瀑布</a> <a href="/tupian/hubo_t11602/" title="湖泊图片">湖泊</a> <a href="/tupian/daoyu_t503/" title="岛屿图片">岛屿</a> <a href="/tupian/haidao_t934/" title="海岛图片">海岛</a> <a href="/tupian/dahai_t660/" title="大海图片">大海</a> <a href="/tupian/hailang_t499/" title="海浪图片">海浪</a> <a href="/tupian/haitan_t1225/" title="海滩图片">海滩</a> <a href="/tupian/shatan_t600/" title="沙滩图片">沙滩</a> <a href="/tupian/haian_t20/" title="海岸图片">海岸</a> <a href="/tupian/haijing_t16177/" title="海景图片">海景</a> <a href="/tupian/chunji_t8857/" title="春季图片">春季</a> <a href="/tupian/xiaji_t3145/" title="夏季图片">夏季</a> <a href="/tupian/qiuji_t3374/" title="秋天图片大全_秋季的图片_秋季图片">秋季</a> <a href="/tupian/dongji_t3144/" title="冬季图片">冬季</a> <a href="/tupian/siji_t5102/" title="四季图片">四季</a> <a href="/tupian/tianqiqihou_t19449/" title="天气气候图片">天气气候</a> <a href="/tupian/bingxuejingse_t19450/" title="冰雪景色图片">冰雪景色</a> <a href="/tupian/bingshan_t471/" title="冰山图片">冰山</a> </div></div><div id="tplistleft"><script>dy("tplistleft");</script></div><ul class="ali"> <li><div class="il_img"><a href="/tupian/yuanyuan_yueliang_v49253/" title="圆圆的月亮图片(14张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/23/yuanyuan_yueliang-014.jpg" alt="圆圆的月亮图片"></a></div><p><a href="/tupian/yuanyuan_yueliang_v49253/" title="圆圆的月亮图片" target="_blank">圆圆的月亮图片(14张)</a></p></li><li><div class="il_img"><a href="/tupian/shanmai_senlin_v49159/" title="多雾的山和茂盛的森林图片(11张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/19/shanmai_senlin.jpg" alt="多雾的山和茂盛的森林图片"></a></div><p><a href="/tupian/shanmai_senlin_v49159/" title="多雾的山和茂盛的森林图片" target="_blank">多雾的山和茂盛的森林图片(11张)</a></p></li><li><div class="il_img"><a href="/tupian/senlin_shuzhi_shugen_v49157/" title="森林里的树枝和树根图片(11张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/19/senlin_shuzhi_shugen-001.jpg" alt="森林里的树枝和树根图片"></a></div><p><a href="/tupian/senlin_shuzhi_shugen_v49157/" title="森林里的树枝和树根图片" target="_blank">森林里的树枝和树根图片(11张)</a></p></li><li><div class="il_img"><a href="/tupian/shandian_v49140/" title="令人害怕的闪电图片(15张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/19/shandian-002.jpg" alt="令人害怕的闪电图片"></a></div><p><a href="/tupian/shandian_v49140/" title="令人害怕的闪电图片" target="_blank">令人害怕的闪电图片(15张)</a></p></li><li><div class="il_img"><a href="/tupian/hubian_fengjing_v49135/" title="湖边的风景图片(12张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/19/hubianfengjing-007.jpg" alt="湖边的风景图片"></a></div><p><a href="/tupian/hubian_fengjing_v49135/" title="湖边的风景图片" target="_blank">湖边的风景图片(12张)</a></p></li><li><div class="il_img"><a href="/tupian/xuanli_caihong_v49124/" title="绚丽的彩虹图片(15张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/18/xuanli_caihong-004.jpg" alt="绚丽的彩虹图片"></a></div><p><a href="/tupian/xuanli_caihong_v49124/" title="绚丽的彩虹图片" target="_blank">绚丽的彩虹图片(15张)</a></p></li><li><div class="il_img"><a href="/tupian/xiyang_v49117/" title="景色秀美的夕阳图片(10张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/17/xiyang-001.jpg" alt="景色秀美的夕阳图片"></a></div><p><a href="/tupian/xiyang_v49117/" title="景色秀美的夕阳图片" target="_blank">景色秀美的夕阳图片(10张)</a></p></li><li><div class="il_img"><a href="/tupian/zhanlan_dahai_v49068/" title="湛蓝的大海图片(14张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/14/zhanlan_dahai-002.jpg" alt="湛蓝的大海图片"></a></div><p><a href="/tupian/zhanlan_dahai_v49068/" title="湛蓝的大海图片" target="_blank">湛蓝的大海图片(14张)</a></p></li><li><div class="il_img"><a href="/tupian/pubu_v49069/" title="蔚为壮观的瀑布图片(11张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/14/pubu-001.jpg" alt="蔚为壮观的瀑布图片"></a></div><p><a href="/tupian/pubu_v49069/" title="蔚为壮观的瀑布图片" target="_blank">蔚为壮观的瀑布图片(11张)</a></p></li><li><div class="il_img"><a href="/tupian/xuanlan_jiguang_v49067/" title="绚烂的极光图片(13张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/14/xuanlan_jiguang.jpg" alt="绚烂的极光图片"></a></div><p><a href="/tupian/xuanlan_jiguang_v49067/" title="绚烂的极光图片" target="_blank">绚烂的极光图片(13张)</a></p></li><li><div class="il_img"><a href="/tupian/caihong_v49005/" title="七色的彩虹图片(15张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/11/caihong-014.jpg" alt="七色的彩虹图片"></a></div><p><a href="/tupian/caihong_v49005/" title="七色的彩虹图片" target="_blank">七色的彩虹图片(15张)</a></p></li><li><div class="il_img"><a href="/tupian/titian_v48958/" title="美丽的梯田风景图片(11张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/09/titian-005.jpg" alt="美丽的梯田风景图片"></a></div><p><a href="/tupian/titian_v48958/" title="美丽的梯田风景图片" target="_blank">美丽的梯田风景图片(11张)</a></p></li><li><div class="il_img"><a href="/tupian/shandian_v48889/" title="恐怖的闪电图片(10张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/06/shandian.jpg" alt="恐怖的闪电图片"></a></div><p><a href="/tupian/shandian_v48889/" title="恐怖的闪电图片" target="_blank">恐怖的闪电图片(10张)</a></p></li><li><div class="il_img"><a href="/tupian/xuanli_caihong_v48869/" title="绚丽的彩虹图片(16张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201805/06/xuanli_caihong-002.jpg" alt="绚丽的彩虹图片"></a></div><p><a href="/tupian/xuanli_caihong_v48869/" title="绚丽的彩虹图片" target="_blank">绚丽的彩虹图片(16张)</a></p></li><li><div class="il_img"><a href="/tupian/titian_v48708/" title="壮观的梯田风景图片(12张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201804/29/titian-005.jpg" alt="壮观的梯田风景图片"></a></div><p><a href="/tupian/titian_v48708/" title="壮观的梯田风景图片" target="_blank">壮观的梯田风景图片(12张)</a></p></li><li><div class="il_img"><a href="/tupian/qiushou_de_maitian_v48681/" title="秋收的麦田风景图片(19张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201804/28/qiushou_de_maitian-015.jpg" alt="秋收的麦田风景图片"></a></div><p><a href="/tupian/qiushou_de_maitian_v48681/" title="秋收的麦田风景图片" target="_blank">秋收的麦田风景图片(19张)</a></p></li><li><div class="il_img"><a href="/tupian/xuanli_caihong_v48641/" title="绚丽的彩虹图片(11张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201804/27/xuanli_caihong-015.jpg" alt="绚丽的彩虹图片"></a></div><p><a href="/tupian/xuanli_caihong_v48641/" title="绚丽的彩虹图片" target="_blank">绚丽的彩虹图片(11张)</a></p></li><li><div class="il_img"><a href="/tupian/matiwan_v48637/" title="美国亚利桑那州马蹄湾图片(11张)" target="_blank"><img src="http://img.ivsky.com/img/tupian/li/201804/27/matiwan-004.jpg" alt="美国亚利桑那州马蹄湾图片"></a></div><p><a href="/tupian/matiwan_v48637/" title="美国亚利桑那州马蹄湾图片" target="_blank">美国亚利桑那州马蹄湾图片(11张)</a></p></li></ul><div id="tplistleft1"><script>dy("tplistleft1");</script></div><div class="pagelist"><a class='page-prev' href='/tupian/ziranfengguang/'>上一页</a><a href='/tupian/ziranfengguang/'>1</a><span class='page-cur'>2</span><a href='/tupian/ziranfengguang/index_3.html'>3</a><a href='/tupian/ziranfengguang/index_4.html'>4</a><a href='/tupian/ziranfengguang/index_5.html'>5</a><a href='/tupian/ziranfengguang/index_6.html'>6</a><a href='/tupian/ziranfengguang/index_7.html'>7</a><a href='/tupian/ziranfengguang/index_8.html'>8</a><a href='/tupian/ziranfengguang/index_9.html'>9</a><a href='/tupian/ziranfengguang/index_10.html'>10</a><a href='/tupian/ziranfengguang/index_11.html'>11</a><a class='page-next' href='/tupian/ziranfengguang/index_3.html'>下一页</a></div><div id="tplistleft2"><script>dy("tplistleft2");</script></div><div id="tplistleft3"><script>dy("tplistleft3");</script></div></div><div class="right"><div id="tplistr1"><script>dy("tplistr1");</script></div> <div class="rb"><div class="rtit">最近更新的小分类</div><div class="htag"><a href="/tupian/richu_t165/" title="日出图片">日出图片</a><a href="/tupian/riluo_t166/" title="日落图片">日落图片</a><a href="/tupian/ziranfengjing_t2800/" title="自然风景图片_自然风景图片大全">自然风景图片</a><a href="/tupian/weimei_t5095/" title="唯美图片_唯美图片大全">唯美图片</a><a href="/tupian/shanmai_t95/" title="山脉图片">山脉图片</a><a href="/tupian/shanchuan_t2213/" title="山川图片">山川图片</a><a href="/tupian/shamo_t2726/" title="沙漠图片">沙漠图片</a><a href="/tupian/huangmo_t35799/" title="荒漠图片">荒漠图片</a><a href="/tupian/yekong_t19444/" title="夜空图片">夜空图片</a><a href="/tupian/yueliang_t20603/" title="月亮图片">月亮图片</a><a href="/tupian/maitian_t268/" title="麦田图片">麦田图片</a><a href="/tupian/lantianbaiyun_t1485/" title="蓝天白云图片">蓝天白云图片</a><a href="/tupian/shanshui_t2212/" title="山水图片_山水图片大全">山水图片</a><a href="/tupian/dahai_t660/" title="大海图片">大海图片</a><a href="/tupian/haian_t20/" title="海岸图片">海岸图片</a><a href="/tupian/yingguo_jushizhen_t42150/" title="英国巨石阵图片">英国巨石阵图片</a><a href="/tupian/xiyang_t15/" title="夕阳图片">夕阳图片</a><a href="/tupian/wanxia_t84/" title="晚霞图片">晚霞图片</a><a href="/tupian/luori_t8846/" title="落日图片">落日图片</a><a href="/tupian/haishui_t1476/" title="海水图片">海水图片</a></div></div> <div class="rb"><div class="rtit">热门的小分类</div><div class="htag"><a href="/tupian/ziranfengjing_t2800/" title="自然风景图片_自然风景图片大全">自然风景图片</a><a href="/tupian/richu_t165/" title="日出图片">日出图片</a><a href="/tupian/caoyuan_t933/" title="草原图片">草原图片</a><a href="/tupian/lantianbaiyun_t1485/" title="蓝天白云图片">蓝天白云图片</a><a href="/tupian/shanshui_t2212/" title="山水图片_山水图片大全">山水图片</a><a href="/tupian/xingkong_t810/" title="星空图片">星空图片</a><a href="/tupian/yangguang_t599/" title="阳光图片">阳光图片</a><a href="/tupian/dahai_t660/" title="大海图片">大海图片</a><a href="/tupian/xiyang_t15/" title="夕阳图片">夕阳图片</a><a href="/tupian/dongji_t3144/" title="冬季图片">冬季图片</a><a href="/tupian/qiuji_t3374/" title="秋天图片大全_秋季的图片_秋季图片">秋季图片</a><a href="/tupian/huangse_t12134/" title="黄色图片">黄色图片</a><a href="/tupian/hongfengye_t12377/" title="红枫叶图片">红枫叶图片</a><a href="/tupian/yekong_t19444/" title="夜空图片">夜空图片</a><a href="/tupian/wanxia_t84/" title="晚霞图片">晚霞图片</a><a href="/tupian/tiankong_t811/" title="天空图片">天空图片</a><a href="/tupian/wu_t11476/" title="雾图片">雾图片</a><a href="/tupian/siji_t5102/" title="四季图片">四季图片</a><a href="/tupian/daziran_t3005/" title="大自然图片">大自然图片</a><a href="/tupian/yueliang_t20603/" title="月亮图片">月亮图片</a></div></div><div id="tplistr2"><script>dy("tplistr2");</script></div></div></div><div class="box"><div class="f_rec"><b>推荐内容:</b><a href="/tupian/ziranfengjing_t2800/" title="自然风景图片_自然风景图片大全">自然风景图片_自然风景图片大全</a><a href="/tupian/weimei_t5095/" title="唯美图片_唯美图片大全">唯美图片_唯美图片大全</a><a href="/tupian/qiuji_t3374/" title="秋天图片大全_秋季的图片_秋季图片">秋天图片大全_秋季的图片_秋季图片</a><a href="/tupian/xuejing_t36/" title="雪景图片_雪景图片大全">雪景图片_雪景图片大全</a><a href="/tupian/qiutian_t62/" title="秋天图片大全_秋天图片">秋天图片大全_秋天图片</a><a href="/tupian/huahai_t3130/" title="花海图片_花海图片大全">花海图片_花海图片大全</a><a href="/tupian/shanshui_t2212/" title="山水图片_山水图片大全">山水图片_山水图片大全</a><a href="/tupian/qiutiande_t29619/" title="秋天的图片_秋天的图片大全">秋天的图片_秋天的图片大全</a><a href="/tupian/feizhuliu_t4775/" title="非主流图片_非主流图片大全">非主流图片_非主流图片大全</a><a href="/tupian/guilinshanshui_t2459/" title="桂林山水图片_桂林山水图片大全">桂林山水图片_桂林山水图片大全</a><div><b>手机版:</b><a href="http://m.ivsky.com/tupian/ziranfengguang/" target="_blank">自然风光图片 - 自然风景图片</a> </div></div></div><div class="box"><div id="tplistbtm"><script>dy("tplistbtm");</script></div><div id="tplistbtm2"><script>dy("tplistbtm2");</script></div><div id="tppagebtm"><script>dy("tppagebtm");</script></div><div id="tppagebtm2"><script>dy("tppagebtm2");</script></div></div><div id="footer"><div class="box"><div id="fl"><dl><dt>关于</dt><dd><a href="/about/about.html" rel="nofollow">关于天堂</a></dd><dd><a href="/about/disclaimer.html" rel="nofollow">免责声明</a></dd></dl><dl><dt>帮助</dt><dd><a href="/about/tougao.html" rel="nofollow">用户投稿</a></dd><dd><a href="/about/faq.html" rel="nofollow">常见问题</a></dd></dl><dl><dt>联系</dt><dd><a href="/about/contact.html" rel="nofollow">联系我们</a></dd><dd><a href="/about/guestbook.html" rel="nofollow">留言反馈</a></dd><dd><a href="/about/ad.html" rel="nofollow">广告投放</a></dd></dl><dl><dt>关注</dt><dd class="sina"><a href="http://weibo.com/ivskycom" target="_blank" rel="nofollow">新浪微博</a></dd></dl></div><div id="fr"><p>© 2005-2018 <a href="http://www.ivsky.com/">天堂图片网</a> <a href="http://www.miibeian.gov.cn/" target="_blank" rel="nofollow">闽ICP备:05021777号</p><p>本站图片收集自网络,仅供个人学习交流使用,版权归原作者所有,请勿用于任何商业用途</p><p>更多信息请浏览本站免责声明</p></div></div></div><script>dy("tbox");</script><div id="tj"><script>dy("tj");</script></div></body></html>"""
html_tree = etree.HTML(html_content)
print(html_tree)
pattern = "//ul[@class='ali']/li/div/a/@href"
pattern2 = "//ul[@class='ali']/li/p/a/text()"
# 图集链接列表 ,图集名
img_set_url_list = html_tree.xpath(pattern)
img_set_name_list = html_tree.xpath(pattern2)
print(img_set_url_list)
print(img_set_name_list)
# 循环获取每个图集下所有缩略图
for i in range(0, len(img_set_url_list)):
print('开始本页第{}个图集'.format(i+1))
# 当前脚本所在目录生成图集名文件夹
if not os.path.exists(os.path.join(basedir, img_set_name_list[i])):
# 根据图集名创建子文件夹
os.mkdir(os.path.join(basedir, img_set_name_list[i]))
print('创建图集目录', os.path.join(basedir, img_set_name_list[i]))
# 请求图集详情页 。 下面的代码跟上节课一样
resp = requests.get(('http://www.ivsky.com' + img_set_url_list[i]))
sub_html_content = resp.text
sub_html_etree = etree.HTML(sub_html_content)
# 获得图片资源src链接
img_src_list = sub_html_etree.xpath("//ul[@class='pli']/li/div/a/img/@src")
img_name_list = sub_html_etree.xpath("//ul[@class='pli']/li/div/a/img/@alt")
print(img_src_list)
for j, img_src in enumerate(img_src_list):
# 请求图片二进制
print('开始下载本图集第{}张图片'.format(j))
resp = requests.get(img_src, timeout=10)
if not resp.status_code == 200:
print('请求失败')
img_bytes = resp.content
print(img_bytes)
# 拼图片绝对路径,保存
print(os.path.join(img_set_name_list[i], f'{j}.jpg'))
with open(os.path.join(img_set_name_list[i], f'{j}.jpg'), mode='wb') as f:
f.write(img_bytes)
break # 注释此行以爬取