#下载HTML文件 import requests url = 'https://example.com/somepage.html' response = requests.get(url, stream=True) if response.status_code == 200: with open('local_file.html', 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) print('HTML文件已成功下载到本地。') else: print(f'Error fetching HTML: {response.status_code}')
#下载csv文件 import requests url = 'https://example.com/data.csv' response = requests.get(url, stream=True) if response.status_code == 200: with open('local_file.csv', 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) print('CSV文件已成功下载到本地。') else: print(f'Error fetching CSV: {response.status_code}')
#下载pdf文件 import requests url = 'https://example.com/document.pdf' response = requests.get(url, stream=True) if response.status_code == 200: with open('local_document.pdf', 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) print('PDF文件已成功下载到本地。') else: print(f'Error fetching PDF: {response.status_code}')