保存视频资源
1 import requests,os 2 kv={'user-agent':'Mozilla/6.0'} 3 root="D:/pics/" 4 5 url="https://v.stu.126.net/mooc-video/nos/mp4/2017/02/28/1005855330_c19061df10ee4cd69496dba4292e9ea3_shd.mp4?ak=285ea3e4dadf82f0450503b24518d5679cdf68ccbb7c2011693b179b27153140698691431388c23472d118fc0d79340ceff6a55d15491982836e42383e13363eec6958477c8e90f2fc976eed060cb9b81e46d140b7b30f910299bee40b26a5c2d9e1e3c44585e5de5b539ccdbe8423a821b91261e44e538d2765af73aa008299a7f5cc498d43fe59a782bc973c30c066b767da1f870bc890754ea6567cb70ca9830b67d08aac63e1ac0c534090a89323f6fd9d4e9030d5d8cb0cb4b5fcb8e77c" 6 path=root+url.split('/')[-1].split('?')[0] 7 try: 8 if not os.path.exists(root): 9 os.mkdir(root) 10 if not os.path.exists(path): 11 r=requests.get(url) 12 else: 13 print("文件已存在") 14 with open(path,'wb') as f: 15 f.write(r.content) 16 f.close() 17 print('文件保存成功') 18 19 # r.raise_for_status() 20 # r.encoding=r.apparent_encoding 21 # print(r.text[:1000]) 22 except Exception as e: 23 print('爬取失败',e)
灵活应用split和os,并且养成检错的习惯