python 下載圖片,音訊。視訊檔案
阿新 • • 發佈:2022-04-13
#下載圖片 def down_picture(url,fileName): try: header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'} proxy_one = commonMethod.getIP() # proxy = '181.121.214.16:14140' proxies = {View Code"https": "https://" + proxy_one } r = requests.get(url, headers=header, proxies=proxies) with open(fileName, 'wb') as f: f.write(r.content) time.sleep(1) except Exception as ex: print(ex) #下載音訊,視訊檔案 def down_audio(url,filename): try: page_size= 1024 * 128 header = { 'Connection': 'keep-alive', 'Host': 'live.xmcdn.com', # 'httpdnsType':'domain', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36', 'Range': 'bytes=0-' + str(page_size), } proxy_one = commonMethod.getIP() # proxy = '181.121.214.16:14140' proxies = { "https": "https://" + proxy_one } response = requests.get(url, headers=header, timeout=60, stream=True) if response.status_code == 200 or response.status_code == 206: # print('採集成功') with(open(fileName, 'ab')) as f: for chunk in response.iter_content(chunk_size=512): if chunk: f.write(chunk) time.sleep(1) except Exception as ex: print(ex)