知乎高清原图下载python极简脚本

209 阅读1分钟

1.脚本,注意修改保存目录和爬取链接

# 爬取的知乎文字
#本例爬取 《已续更wlop鬼刀高清壁纸系列4K8K都有不多说看图全知乎放上来最多最好的好吧✧*。٩(ˊωˋ*)و✧*鬼刀wlop》
import requests
import os
from lxml import etree

dir_name = 'pic1' #1.需要修改:设置需要保存的文件夹的名字
 
if not os.path.exists(dir_name): #os模块判断文件夹是否存在并创建
    os.mkdir(dir_name)

link= 'https://zhuanlan.zhihu.com/p/107129943' #2.需要指定爬取的文章链接


headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                      '(KHTML, like Gecko) Chrome/87.0.4280.88 '
                      'Safari/537.36 Edg/87.0.664.66'}
r=requests.get(url=link,headers=headers)

html=etree.HTML(r.content)
title_list=html.xpath('//figure[*]/img')
for item in title_list:
    img_url = item.xpath('./@data-original')[0]
    print(img_url)
    picture_name = img_url.split('/')[-1].split('?')[0]
    print('picture_name:' + picture_name)
    reponse = requests.get(img_url)
    with open('./'+ dir_name + '/'+picture_name,'wb') as f:
        f.write(reponse.content)

2.爬取结果,均为高清图

image.png

3.脚本执行效果

image.png 原文地址: