Để download file từ 1 trang web bất kỳ chúng ta có rất nhiều cách. Sau đây vinasupport.com sẽ hướng dẫn các bạn download file ảnh, video, … từ 1 website bất kỳ về máy tính Linux sử dụng curl command line.
Kiểm tra thông tin của file download
Trước khi download bạn có thể kiểm tra thông tin của file download như:
curl -I //vinasupport.com/assets/img/vinasupport_logo.png
Kết quả:
Hoặc chỉ mã status code
curl -s -I -w "%{http_code}" //vinasupport.com/assets/img/vinasupport_logo.png -o /dev/null
Với:
- 200: File hoạt động bình thường
- 404: File không tồn tại
- …
Download file với curl command
Sử dụng command sau để download 1 file
curl --output
VD:
curl //vinasupport.com/assets/img/vinasupport_logo.png --output vinasupport.png
Sử dụng option:
- –silent: để ko show output [stdout]
Nguồn: vinasupport.com
Bạn có thể download file [File ảnh, File text, …] từ 1 đường dẫn trên web sử dụng thư viện urllib của Python 3.
Đoạn source code Python 3 như sau:
import urllib import os output_dir = '/tmp' image_url = '//vinasupport.com/assets/img/vinasupport_logo.png' # Make output directory if not exist if not os.path.exists[output_dir]: os.makedirs[output_dir] # save path image_save_path = output_dir + '/' + os.path.basename[image_url] # Download file from url urllib.request.urlretrieve[image, image_save_path] print[image_save_path]
Trong ví dụ trên, mình thực hiện download file ảnh có đường dẫn là: //vinasupport.com/assets/img/vinasupport_logo.png
Và lưu nó xuống dường dẫn trên server /tmp/vinasupport_logo.png
Một cách khác là sử dụng thư viện requests
Đầu tiên cài đặt requests thông qua trình quản lý module / packages của Python 3
pip install requests
Sử dụng requests để download file:
from requests import get # to make GET request def download[url, file_name]: # open in binary mode with open[file_name, "wb"] as file: # get request response = get[url] # write to file file.write[response.content]
Nguồn: vinasupport.com
I would like to send a complex curl command into python [3.9.7] script to download an output file .tif I use a TimeNum variable because i want to download different files and clip the .tif in a personal area of intrest [I want to use a for loop].
The curl command is something like this:
curl --data '{"productType":"VMI","productDate":"1648710600000"}' -H "Content-Type: application/json" -X POST //example.it/wide/product/downloadProduct --output hrd2.tif
I try different solutions:
1]
import shlex
import subprocess
TimeNum=1648710600000
cmd ='''curl --data \'{"productType":"VMI","productDate":"%s"}\' -H "Content-Type: application/json" -X POST //example.it/wide/product/downloadProduct --output %s.tif''' % [TimeNum,TimeNum]
args = shlex.split[cmd]
process = subprocess.Popen[args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE]
stdout, stderr = process.communicate[]
The download work but the output takes up 1KB instead of about 11MB, and an error occur when i try to open it in Windows.
- I write a txt file with a list of curl command line by line and then:
file = open['CURL_LIST.txt', 'r']
lines = file.readlines[]
for line in enumerate[lines]:
os.system[line.strip[]]
but don't work fine and i have the same output of case 1 above. Now i try to use urllib.request but i'm not able to use it very well.
Someone have a suggestion? Thanks in advance for your help