"""
该代码仅供交流与测试,如有非法盈利行为,请自行负责
作者:yhkdw
hello world
"""
import json
import urllib.request
import time
from lxml import etree
headers={
'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'accept-language':'zh-CN,zh;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
'cache-control':'max-age=0',
'cookie':'有用的cookie',
'sec-ch-ua-mobile':'?0',
'sec-ch-ua-platform':'"Windows"',
'sec-fetch-dest':'document',
'sec-fetch-mode':'navigate',
'sec-fetch-site':'same-origin',
'sec-fetch-user':'?1',
'upgrade-insecure-requests':'1',
'user-agent':'有用的ua',
}
# url='https://www.xiaohongshu.com/explore/6149f62f000000000102d0cd'
#输入下载链接
url=input('请输入你要下载的图片链接')
request=urllib.request.Request(url=url,headers=headers)
response=urllib.request.urlopen(request)
content=response.read().decode('utf-8')
# print(content)
#找到起始和结束的url
begin=content.find('imageList')+11
new_content=content[begin::]
new_end=new_content.find('],"')
# end=content.find('],"')
# json_img=content[begin:end+1]
# 转成json
json_img=new_content[:new_end+1:]
# print(json_img)
try:
image_json=json.loads(json_img)
i=1
for image in image_json:
if image.get('url') and image.get('traceId'):
print('正在下载' + str(i) + '图片')
img_url=image['url'][:30]+image['traceId']
urllib.request.urlretrieve(img_url,'F://redbook//'+image['traceId']+'.webp')
time.sleep(1)
i+=1
except:
print('服务器在升级')