1. 程式人生 > 其它 >python 下載圖片,音訊。視訊檔案

python 下載圖片,音訊。視訊檔案

#下載圖片
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 = {
            
"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)
View Code