[Python] 纯文本查看 复制代码 import requests
import re
import time,datetime
import os
'''说明:本代码,是调用第三方接口开实现小红书图片无水印下载,并非是本人针对小红书做的接口开发'''
'''获取小红书无水印视频下载链接'''
def xhslinks(data):
url = f'https://dlpanda.com/zh-CN/xhs?url={data}'
res = requests.get(url).text
'''res 里面包含了网页的全部信息,如果想要获取文案内容,或者是标题内容,请自行提取,此处不做该内容的提取,本文只提取图片内容'''
aaa = [r.span() for r in re.finditer('<img alt="" src="', res)]
bbb = [r.span() for r in re.finditer('" width="100%"', res)]
img_links = []
for i in range(len(aaa)):
link = res[aaa[i][1]:bbb[i][0]].find('https')
if link == -1:
links = f'https:{res[aaa[i][1]:bbb[i][0]]}'
else:
links = res[aaa[i][1]:bbb[i][0]]
img_links.append(links)
print(img_links)
return img_links
'''下载图片'''
def download(path, picture_url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE",
}
start = time.time()
for i in range(len(picture_url)):
times = datetime.datetime.now()
with open(rf'{path}\{i}.jpg', 'wb') as v:
try:
v.write(requests.get(url=picture_url[i], headers=headers).content)
end = time.time()
cost = end - start
print(f'{path} 文件下载成功,耗时: {cost} s')
except Exception as e:
print('图片下载错误!')
def GetDesktopPath():
return os.path.join(os.path.expanduser("~"), 'Desktop')
if __name__ == '__main__':
while True:
times = datetime.datetime.now()
'''此处保存图片的路径,需要自行进行修改'''
filename = rf'C:\Users\OneDrive\桌面\小红书图片下载'
# data = '24 阿九的穿戴甲发布了一篇小红书笔记,快来看吧! 😆 LAAVoM94WRRJ8z7 😆 http://xhslink.com/NYLJam,复制本条信息,打开【小红书】App查看精彩内容!'
data = '85 阿九的穿戴甲发布了一篇小红书笔记,快来看吧! 😆 f6AoQf34cRrkpob 😆 http://xhslink.com/ZWGQqm,复制本条信息,打开【小红书】App查看精彩内容!'
# data = '78 阿九的穿戴甲发布了一篇小红书笔记,快来看吧! 😆 BwJuIn0JpxR1qxM 😆 http://xhslink.com/8kSQqm,复制本条信息,打开【小红书】App查看精彩内容!'
'''此处输入网页链接,或者是小红书链接,皆可,该接口会自动分析,无需人工处理'''
# data = input('请输入小红书链接:')
picture_url = xhslinks(data)
day = time.strftime("%Y%m%d", time.localtime())
tiemss = time.strftime("%H%M%S", time.localtime())
if os.path.exists(rf'{filename}\{day}\{tiemss}'):
# os.mkdir(path=filename)
if os.path.exists(rf'{filename}\{day}\{tiemss}'):
print('桌面文件夹已存在')
else:
os.makedirs(rf'{filename}\{day}\{tiemss}')
download(rf'{filename}\{day}\{tiemss}', picture_url) |