Python wget thanh tiến trình tải xuống

Trong một bài đăng gần đây về việc tải xuống các tệp trong Python, chúng tôi đã học cách sử dụng mô-đun requests để kiểm tra các tài nguyên có thể tải xuống và hạn chế một số biện pháp để lấy các tệp từ xa. Để nâng cao kiến ​​thức của chúng ta về chủ đề này, tôi sẽ cung cấp cho bạn một phần giới thiệu khác để bạn làm quen với mô-đun wget đó là việc triển khai lệnh wget đi kèm với hệ điều hành. Bây giờ, hãy bắt đâù

Mục lục

Lệnh wget là gì?

Lệnh wget là một tiện ích không tương tác để tải xuống các tệp từ xa từ internet được tích hợp sẵn với các hệ điều hành dựa trên Unix. Nó hỗ trợ các giao thức HTTP, HTTPS và FTP, cũng như truy xuất thông qua proxy HTTP

Kêu gọi

Theo mặc định, Wget rất đơn giản để gọi. Cú pháp cơ bản là

wget [options].. [URL]

Wget sẽ chỉ cần tải xuống tất cả URL được chỉ định trên dòng lệnh. URL là Bộ định vị tài nguyên thống nhất, như được định nghĩa bên dưới

Không quá khó để tìm hiểu các tùy chọn của lệnh wget. Sử dụng

OPTIONS
   Option Syntax
       Since Wget uses GNU getopt to process command-line arguments, every option has a long form along with the short one.  Long options are more
       convenient to remember, but take time to type.  You may freely mix different option styles, or specify options after the command-line
       arguments.  Thus you may write:

               wget -r --tries=10 //fly.srk.fer.hr/ -o log

       The space between the option accepting an argument and the argument may be omitted.  Instead of -o log you can write -olog.

       You may put several options that do not require arguments together, like:

               wget -drc 

       This is completely equivalent to:

               wget -d -r -c 

       Since the options can be specified after the arguments, you may terminate them with --.  So the following will try to download URL -x,
       reporting failure to log:

               wget -o log -- -x

       The options that accept comma-separated lists all respect the convention that specifying an empty list clears its value.  This can be
       useful to clear the .wgetrc settings.  For instance, if your .wgetrc sets "exclude_directories" to /cgi-bin, the following example will
       first reset it, and then set it to exclude /~nobody and /~somebody.  You can also clear the lists in .wgetrc.
2, bạn có thể nhận hướng dẫn của lệnh này. Dưới đây là một ví dụ được trích xuất từ ​​đầu ra của việc chạy
OPTIONS
   Option Syntax
       Since Wget uses GNU getopt to process command-line arguments, every option has a long form along with the short one.  Long options are more
       convenient to remember, but take time to type.  You may freely mix different option styles, or specify options after the command-line
       arguments.  Thus you may write:

               wget -r --tries=10 //fly.srk.fer.hr/ -o log

       The space between the option accepting an argument and the argument may be omitted.  Instead of -o log you can write -olog.

       You may put several options that do not require arguments together, like:

               wget -drc 

       This is completely equivalent to:

               wget -d -r -c 

       Since the options can be specified after the arguments, you may terminate them with --.  So the following will try to download URL -x,
       reporting failure to log:

               wget -o log -- -x

       The options that accept comma-separated lists all respect the convention that specifying an empty list clears its value.  This can be
       useful to clear the .wgetrc settings.  For instance, if your .wgetrc sets "exclude_directories" to /cgi-bin, the following example will
       first reset it, and then set it to exclude /~nobody and /~somebody.  You can also clear the lists in .wgetrc.
2

OPTIONS
   Option Syntax
       Since Wget uses GNU getopt to process command-line arguments, every option has a long form along with the short one.  Long options are more
       convenient to remember, but take time to type.  You may freely mix different option styles, or specify options after the command-line
       arguments.  Thus you may write:

               wget -r --tries=10 //fly.srk.fer.hr/ -o log

       The space between the option accepting an argument and the argument may be omitted.  Instead of -o log you can write -olog.

       You may put several options that do not require arguments together, like:

               wget -drc 

       This is completely equivalent to:

               wget -d -r -c 

       Since the options can be specified after the arguments, you may terminate them with --.  So the following will try to download URL -x,
       reporting failure to log:

               wget -o log -- -x

       The options that accept comma-separated lists all respect the convention that specifying an empty list clears its value.  This can be
       useful to clear the .wgetrc settings.  For instance, if your .wgetrc sets "exclude_directories" to /cgi-bin, the following example will
       first reset it, and then set it to exclude /~nobody and /~somebody.  You can also clear the lists in .wgetrc.

ví dụ

ví dụ 1. Tải xuống phần mềm PowerISO không cung cấp bất kỳ tùy chọn nào

Chúng tôi thường tải xuống các tệp và để lại tên của chúng do chủ sở hữu gọi chúng. Để làm như vậy, cú pháp rất đơn giản

wget //d32si1eewy6hfa.cloudfront.net/ov2so5=3ex040/PowerISO7-x64.exe

Lệnh trên sẽ tải xuống tệp PowerISO7-x64. exe nằm ở URL được đề cập. Kết quả là tệp đó tại thư mục làm việc hiện tại có tên PowerISO7-x64. người cũ

ví dụ 2. Sử dụng lệnh Wget để lưu tệp đã tải xuống dưới tên khác

Trong thực tế, chúng tôi thường lưu tệp tải xuống vào một nơi và đổi tên thành tên sở thích và có ý nghĩa. Để có được nhu cầu này, chúng ta có thể thực hiện với tùy chọn

OPTIONS
   Option Syntax
       Since Wget uses GNU getopt to process command-line arguments, every option has a long form along with the short one.  Long options are more
       convenient to remember, but take time to type.  You may freely mix different option styles, or specify options after the command-line
       arguments.  Thus you may write:

               wget -r --tries=10 //fly.srk.fer.hr/ -o log

       The space between the option accepting an argument and the argument may be omitted.  Instead of -o log you can write -olog.

       You may put several options that do not require arguments together, like:

               wget -drc 

       This is completely equivalent to:

               wget -d -r -c 

       Since the options can be specified after the arguments, you may terminate them with --.  So the following will try to download URL -x,
       reporting failure to log:

               wget -o log -- -x

       The options that accept comma-separated lists all respect the convention that specifying an empty list clears its value.  This can be
       useful to clear the .wgetrc settings.  For instance, if your .wgetrc sets "exclude_directories" to /cgi-bin, the following example will
       first reset it, and then set it to exclude /~nobody and /~somebody.  You can also clear the lists in .wgetrc.
4 [chữ hoa của chữ o] như

wget -O latest-hugo.zip //github.com/gohugoio/hugo/archive/master.zip

Lệnh trên sẽ lưu tệp zip hugo mới nhất từ ​​GitHub dưới dạng 

OPTIONS
   Option Syntax
       Since Wget uses GNU getopt to process command-line arguments, every option has a long form along with the short one.  Long options are more
       convenient to remember, but take time to type.  You may freely mix different option styles, or specify options after the command-line
       arguments.  Thus you may write:

               wget -r --tries=10 //fly.srk.fer.hr/ -o log

       The space between the option accepting an argument and the argument may be omitted.  Instead of -o log you can write -olog.

       You may put several options that do not require arguments together, like:

               wget -drc 

       This is completely equivalent to:

               wget -d -r -c 

       Since the options can be specified after the arguments, you may terminate them with --.  So the following will try to download URL -x,
       reporting failure to log:

               wget -o log -- -x

       The options that accept comma-separated lists all respect the convention that specifying an empty list clears its value.  This can be
       useful to clear the .wgetrc settings.  For instance, if your .wgetrc sets "exclude_directories" to /cgi-bin, the following example will
       first reset it, and then set it to exclude /~nobody and /~somebody.  You can also clear the lists in .wgetrc.
5 thay vì tên ban đầu

Đọc thêm về lệnh này tại https. //www. gnu. tổ chức/phần mềm/wget/thủ công/wget. html

Giới thiệu ngắn về mô-đun wget

Để cung cấp API cho cộng đồng các nhà phát triển Python, mô-đun wget của nhà phát triển và bảo trì hy vọng sẽ giảm bớt các ứng dụng và triển khai lệnh wget bằng Python. Phiên bản mới nhất đã được phát hành từ năm 2015. Lần cập nhật cuối cùng được thực hiện vào tháng 10 năm 2015 [https. //pypi. tổ chức/dự án/wget/]. Tại thời điểm viết bài, kho lưu trữ mã nguồn dự án trong bitbucket không còn nữa. Kho lưu trữ mã nguồn có thể đã được di chuyển hoặc xóa

Tôi đã tìm thấy kho lưu trữ này trong GitHub, được phân nhánh từ một trong những chủ sở hữu của mô-đun này. Miễn là phiên bản mới nhất được phát hành, không ai tiếp tục duy trì và phát triển mô-đun

Cách tải xuống tệp với wget

Cách sử dụng

________số 8_______

tùy chọn

-o –TỆP đầu ra. Tên tệp hoặc thư mục đầu ra DIR

Sử dụng API

>>> import wget 
>>> url = '//www.futurecrew.com/skaven/song_files/mp3/razorback.mp3' 
>>> filename = wget.download[url] 100% [................................................] 3841532 / 3841532
>>> filename
'razorback.mp3'

Độ lệch mà bạn thấy ở trên là một tác dụng phụ đã được ghi lại. Thanh tiến trình thay thế

>>> wget.download[url, bar=bar_thermometer]

Đối số

OPTIONS
   Option Syntax
       Since Wget uses GNU getopt to process command-line arguments, every option has a long form along with the short one.  Long options are more
       convenient to remember, but take time to type.  You may freely mix different option styles, or specify options after the command-line
       arguments.  Thus you may write:

               wget -r --tries=10 //fly.srk.fer.hr/ -o log

       The space between the option accepting an argument and the argument may be omitted.  Instead of -o log you can write -olog.

       You may put several options that do not require arguments together, like:

               wget -drc 

       This is completely equivalent to:

               wget -d -r -c 

       Since the options can be specified after the arguments, you may terminate them with --.  So the following will try to download URL -x,
       reporting failure to log:

               wget -o log -- -x

       The options that accept comma-separated lists all respect the convention that specifying an empty list clears its value.  This can be
       useful to clear the .wgetrc settings.  For instance, if your .wgetrc sets "exclude_directories" to /cgi-bin, the following example will
       first reset it, and then set it to exclude /~nobody and /~somebody.  You can also clear the lists in .wgetrc.
8 xác định chuỗi thanh tiến trình kiểu nhiệt kế. đối số `tổng` không được bằng 0. Kích thước tối thiểu của thanh được trả về là 3

Thí dụ

[………. ]

Không bao gồm các ký hiệu điều khiển và dấu [và dấu cách]. Xem `bar_adaptive` để biết thêm thông tin

Tùy chỉnh thanh tiến trình

Phương thức

OPTIONS
   Option Syntax
       Since Wget uses GNU getopt to process command-line arguments, every option has a long form along with the short one.  Long options are more
       convenient to remember, but take time to type.  You may freely mix different option styles, or specify options after the command-line
       arguments.  Thus you may write:

               wget -r --tries=10 //fly.srk.fer.hr/ -o log

       The space between the option accepting an argument and the argument may be omitted.  Instead of -o log you can write -olog.

       You may put several options that do not require arguments together, like:

               wget -drc 

       This is completely equivalent to:

               wget -d -r -c 

       Since the options can be specified after the arguments, you may terminate them with --.  So the following will try to download URL -x,
       reporting failure to log:

               wget -o log -- -x

       The options that accept comma-separated lists all respect the convention that specifying an empty list clears its value.  This can be
       useful to clear the .wgetrc settings.  For instance, if your .wgetrc sets "exclude_directories" to /cgi-bin, the following example will
       first reset it, and then set it to exclude /~nobody and /~somebody.  You can also clear the lists in .wgetrc.
9 có đối số
OPTIONS
   Option Syntax
       Since Wget uses GNU getopt to process command-line arguments, every option has a long form along with the short one.  Long options are more
       convenient to remember, but take time to type.  You may freely mix different option styles, or specify options after the command-line
       arguments.  Thus you may write:

               wget -r --tries=10 //fly.srk.fer.hr/ -o log

       The space between the option accepting an argument and the argument may be omitted.  Instead of -o log you can write -olog.

       You may put several options that do not require arguments together, like:

               wget -drc 

       This is completely equivalent to:

               wget -d -r -c 

       Since the options can be specified after the arguments, you may terminate them with --.  So the following will try to download URL -x,
       reporting failure to log:

               wget -o log -- -x

       The options that accept comma-separated lists all respect the convention that specifying an empty list clears its value.  This can be
       useful to clear the .wgetrc settings.  For instance, if your .wgetrc sets "exclude_directories" to /cgi-bin, the following example will
       first reset it, and then set it to exclude /~nobody and /~somebody.  You can also clear the lists in .wgetrc.
8 cho biết thanh tiến trình trông như thế nào. Theo mặc định, thanh tiến trình là một loạt các dấu chấm. Chúng tôi có thể tùy chỉnh nó bằng cách áp dụng ý tưởng được trình bày trong đoạn trích bên dưới

def bar_custom[current, total, width=80]:
    print["Downloading: %d%% [%d / %d] bytes" % [current / total * 100, current, total]]

wget.download['//download.geonames.org/export/zip/US.zip', bar=bar_custom]
Kết luận

Trong bài đăng này, tôi vừa trình bày phương pháp được sử dụng phổ biến nhất để tải xuống tệp trong Python. Cá nhân tôi thích sử dụng mô-đun requests để tải xuống các tệp do sự kết hợp giữa tính đơn giản và sức mạnh của nó. Tuy nhiên, dự án của bạn có thể có những ràng buộc ngăn bạn sử dụng thư viện của bên thứ 3, trong trường hợp đó, tôi sẽ sử dụng mô-đun

wget //d32si1eewy6hfa.cloudfront.net/ov2so5=3ex040/PowerISO7-x64.exe
2 [đối với Python 2] hoặc mô-đun 
wget //d32si1eewy6hfa.cloudfront.net/ov2so5=3ex040/PowerISO7-x64.exe
3 [đối với Python 3]

Bạn thích thư viện nào hơn và tại sao?

Người giới thiệu

[1] GNU Wget 1. 20 Sách hướng dẫn, https. //www. gnu. tổ chức/phần mềm/wget/thủ công/wget. html, truy cập ngày 6. 9. 2020

[1] quên 3. 3 trên PyPI, https. //pypi. org/project/wget/, truy cập ngày 6. 9. 2020

[3] Tải xuống các tệp có tiến trình trong Python, https. //trung bình. com/@petehouston/download-files-with-progress-in-python-96f14f6417a2, truy cập ngày 4. 9. 2020

Chủ Đề