本次是在django中完成
静态文件目录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0d3feadd5f7e6e02ec447bfb812a561c.png)
拼接路径
图片路径
image=os.path.join(STATICFILES_DIRS[0],"audio") # os.path.join() 将多个路径拼接成一个完整的路径
视频路径
l_mp4 = os.path.join(STATICFILES_DIRS[0],"video")
①将下载的图片 保存进audio文件内
imga = requests.get(图片的链接)
image 是刚才拼接的路径 (不写路径使用默认路径 默认路径是当前运行文件的路径)
with open(image + "\{}".format(图片名称) + '.jpg', mode='wb') as f:
f.write(imga.content)
②将下载的视频 保存进video文件内
response = requests.get(视频链接)
l_mp4 是拼接好的路径 这里用到了f
with open(f'{l_mp4}\{图片名称}.mp4', 'wb')as f:
f.write(response.content)
f.flush() #强行把缓冲区中的内容放到磁盘中
保存效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1aadec7c55d0ff7e388218b7d464b0b6.png)
附上 爬虫代码
import requests
from lxml import etree
import re
import time
import os
url='http://wallpaper.upupoo.com/store/browVi/1-0-0-1.htm'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
response=requests.get(url,headers=headers)
html=etree.HTML(response.content.decode())
listTitle=html.xpath("/html/body/div[2]/div[4]/dl/dd/div[2]/p/text()")
listurl = html.xpath('/html/body/div[2]/div[4]/dl/dd/div[1]/div/a/@href')
listU=[]
for index,i in enumerate(listurl):
l_url = 'http://wallpaper.upupoo.com{}'.format(i)
time.sleep(1)
res = requests.get(url=l_url)
urls = re.findall("http://source.upupoo.com.*\.mp4", res.text)
imgs = re.findall("http://source.upupoo.com.*\.jpg",res.text)
img= listTitle[index]
image=os.path.join(STATICFILES_DIRS[0],"audio")
l_mp4 = os.path.join(STATICFILES_DIRS[0],"video")
print("第%d个图片下载中**" %(index + 1))
print(imgs[index])
imga = requests.get(imgs[index])
with open(image + "\{}".format(listTitle[index]) + '.jpg', mode='wb') as f:
f.write(imga.content)
print("第%d个图片下载完成" % (index + 1))
print("第{}个视频下载中**".format(index + 1))
print(urls[0], listTitle[index])
response = requests.get(urls[0])
with open(f'{l_mp4}\{listTitle[index]}.mp4', 'wb')as f:
f.write(response.content)
f.flush()
print("第%d个视频下载完成"%(index+1))
保存时注意路径
往期推荐