使用requests库爬取网络图片、视频、音乐并存储
【摘要】 使用网络爬虫获取网络图片并存储
import requests
import os
import uuid
def download_and_store_image(url): try: # 根据目录 root = "/home/wong/Desktop/images/" # 文件名 path = root + str(uuid.uuid4())+".jpg" #...
使用网络爬虫获取网络图片并存储
import requests
import os
import uuid
def download_and_store_image(url): try: # 根据目录 root = "/home/wong/Desktop/images/" # 文件名 path = root + str(uuid.uuid4())+".jpg" # 根目录不存创建根目录 if not os.path.exists(root): os.mkdir(root) if not os.path.exists(path): r = requests.get(url) r.raise_for_status() with open(path, "wb") as f: # r.content获取的是二进制形式的内容,把二进制数据写入文件 f.write(r.content) f.close() print("文件保存成功") except: print("文件保存失败")
if __name__ == "__main__": url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563543616448&di=7c0d25c3737d437cc457efaa4ae1ca82&imgtype=0&src=http%3A%2F%2Fwww.chinadaily.com.cn%2Fculture%2Fart%2Fimg%2Fattachement%2Fjpg%2Fsite181%2F20150529%2F180373cf843216d2ffe52f.jpg" download_and_store_image(url)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
使用网络爬虫获取网络视频并存储
import requests
import os
import uuid
def download_and_store_image(url): try: # 根据目录 root = "/home/wong/Desktop/videos/" # 文件名 path = root + str(uuid.uuid4())+".mp4" # 根目录不存创建根目录 if not os.path.exists(root): os.mkdir(root) if not os.path.exists(path): r = requests.get(url) r.raise_for_status() with open(path, "wb") as f: # r.content获取的是二进制形式的内容,把二进制数据写入文件 f.write(r.content) f.close() print("文件保存成功") except: print("文件保存失败")
if __name__ == "__main__": url = "http://111.231.191.26/See%20You%20Again.mp4" download_and_store_image(url)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
使用网络爬虫获取网络音乐并存储
import requests
import os
import uuid
def download_and_store_image(url): try: # 根据目录 root = "/home/wong/Desktop/music/" # 文件名 path = root + str(uuid.uuid4())+".mp3" # 根目录不存创建根目录 if not os.path.exists(root): os.mkdir(root) if not os.path.exists(path): r = requests.get(url) r.raise_for_status() with open(path, "wb") as f: # r.content获取的是二进制形式的内容,把二进制数据写入文件 f.write(r.content) f.close() print("文件保存成功") except: print("文件保存失败")
if __name__ == "__main__": url = "https://webfs.yun.kugou.com/201907191936/21ad09311b751436c5612c86358dbc58/G162/M0B/0C/01/gpQEAFzQ_H6AUcGxADW6o5XaYZU550.mp3" download_and_store_image(url)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/96493421
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)