栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用xpath批量爬取堆糖图片

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用xpath批量爬取堆糖图片

kw input( 输入搜索的关键字 ) url https://www.duitang.com/search/?kw {} type feed .format(kw) headers { User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 , Accept : text/html,application/xhtml xml,application/xml;q 0.9,image/avif,image/webp,image/apng,*/*;q 0.8,application/signed-exchange;v q 0.9 # 确定文件夹路径 base_path os.path.dirname(__file__) path os.path.join(base_path, {} .format(kw)) if not os.path.exists(path): os.makedirs(path) # print(path) html requests.get(url, headers headers) xhtml etree.HTML(html.text) src xhtml.xpath( //a[ class a ]/img/ src ) title xhtml.xpath( //a[ class a ]/img/ data-rootid ) # print(title) for i in range(len(src)): img_src requests.get(url src[i], headers headers).content pic_path path ./{} .format(kw) title[i] .jpg with open(pic_path, wb ) as f: f.write(img_src) print( 正在保存第{}张 剩{}张 .format(i 1, len(src) - i - 1))

1.输入搜索关键字

2.生成关键字文件夹

3.批量保存图片到对应的文件夹中

效果如下

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267914.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号