Python yêu cầu tải xuống luồng

Đoạn mã nhỏ được viết ở trên này sẽ tải xuống hình ảnh sau từ web. Bây giờ hãy kiểm tra thư mục cục bộ của bạn [thư mục chứa tập lệnh này] và bạn sẽ tìm thấy hình ảnh này

Tất cả những gì chúng ta cần là URL của nguồn hình ảnh. [Bạn có thể lấy URL của nguồn hình ảnh bằng cách nhấp chuột phải vào hình ảnh và chọn tùy chọn Xem hình ảnh. ]

Tải xuống các tệp lớn

Nội dung phản hồi HTTP [r. content] không là gì ngoài một chuỗi đang lưu trữ dữ liệu tệp. Vì vậy, sẽ không thể lưu tất cả dữ liệu trong một chuỗi trong trường hợp tệp lớn. Để khắc phục vấn đề này, chúng tôi thực hiện một số thay đổi đối với chương trình của mình

  • Vì tất cả dữ liệu tệp không thể được lưu trữ bằng một chuỗi, nên chúng tôi sử dụng r. phương thức iter_content để tải dữ liệu theo khối, chỉ định kích thước khối
  •  r = requests.get[URL, stream = True]

    Đặt tham số luồng thành True sẽ chỉ tải xuống các tiêu đề phản hồi và kết nối vẫn mở. Điều này tránh đọc tất cả nội dung cùng một lúc vào bộ nhớ cho các phản hồi lớn. Một đoạn cố định sẽ được tải mỗi lần trong khi r. iter_content được lặp lại

    Đây là một ví dụ




    import requests

    import8= requests0

     

     r = requests.get[URL, stream = True]
    0= requests4= requests6requests7

     

     r = requests.get[URL, stream = True]
    7
     r = requests.get[URL, stream = True]
    8
     r = requests.get[URL, stream = True]
    9image_url2# imported the requests library1image_url4image_url5

    # imported the requests library5image_url7 image_url8_______91_______9 =0==2=3

     

    =5=6

    =5=8 =9

    "//www.python.org/static/community_logos/python-logo-master-v3-TM.png"0"//www.python.org/static/community_logos/python-logo-master-v3-TM.png"1

    Đang tải xuống video

    Trong ví dụ này, chúng tôi quan tâm đến việc tải xuống tất cả các video bài giảng có sẵn trên trang web này. Tất cả các kho lưu trữ của bài giảng này có sẵn ở đây. Vì vậy, trước tiên chúng tôi quét trang web để trích xuất tất cả các liên kết video và sau đó tải xuống từng video một




    import "//www.python.org/static/community_logos/python-logo-master-v3-TM.png"3

    "//www.python.org/static/community_logos/python-logo-master-v3-TM.png"4 "//www.python.org/static/community_logos/python-logo-master-v3-TM.png"5import "//www.python.org/static/community_logos/python-logo-master-v3-TM.png"7

     

    "//www.python.org/static/community_logos/python-logo-master-v3-TM.png"9

     0

     1

     2

     3

     4

     5

     

     7

     8= # URL of the image to be downloaded is defined as image_url0

     

    # URL of the image to be downloaded is defined as image_url2 # URL of the image to be downloaded is defined as image_url3

    # URL of the image to be downloaded is defined as image_url4

    # imported the requests library5# URL of the image to be downloaded is defined as image_url6

    # imported the requests library5

     r = requests.get[URL, stream = True]
    0=
     r = requests.get[URL, stream = True]
    00

    # URL of the image to be downloaded is defined as image_url4

    # imported the requests library5

     r = requests.get[URL, stream = True]
    03

    # imported the requests library5

     r = requests.get[URL, stream = True]
    05=
     r = requests.get[URL, stream = True]
    07_______0_______08
     r = requests.get[URL, stream = True]
    09

    # URL of the image to be downloaded is defined as image_url4

    # imported the requests library5

     r = requests.get[URL, stream = True]
    12

    # imported the requests library5

     r = requests.get[URL, stream = True]
    14=
     r = requests.get[URL, stream = True]
    16
     r = requests.get[URL, stream = True]
    17
     r = requests.get[URL, stream = True]
    09

     

    # imported the requests library5

     r = requests.get[URL, stream = True]
    21

    # imported the requests library5

     r = requests.get[URL, stream = True]
    23=
     r = requests.get[URL, stream = True]
    25_______0_______26
     r = requests.get[URL, stream = True]
    27
     r = requests.get[URL, stream = True]
    28
     r = requests.get[URL, stream = True]
    29image_url7
     r = requests.get[URL, stream = True]
    31image_url9
     r = requests.get[URL, stream = True]
    14=8
     r = requests.get[URL, stream = True]
    27
     r = requests.get[URL, stream = True]
    28
     r = requests.get[URL, stream = True]
    37_______0_______38_______0_______39

     

    # imported the requests library5

     r = requests.get[URL, stream = True]
    42
     r = requests.get[URL, stream = True]
    43

     

     

    # URL of the image to be downloaded is defined as image_url2

     r = requests.get[URL, stream = True]
    47

     

    # imported the requests library5image_url7

     r = requests.get[URL, stream = True]
    31image_url9
     r = requests.get[URL, stream = True]
    53

     

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    56

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    58

    =5

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    61

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    63

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    65=
     r = requests.get[URL, stream = True]
    67
     r = requests.get[URL, stream = True]
    68
     r = requests.get[URL, stream = True]
    69
     r = requests.get[URL, stream = True]
    70
     r = requests.get[URL, stream = True]
    71
     r = requests.get[URL, stream = True]
    72

     

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    75
     r = requests.get[URL, stream = True]
    9
     r = requests.get[URL, stream = True]
    77
     r = requests.get[URL, stream = True]
    78
     r = requests.get[URL, stream = True]
    79

    =5

     r = requests.get[URL, stream = True]
    55# URL of the image to be downloaded is defined as image_url6

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    0=
     r = requests.get[URL, stream = True]
    86= requests6
     r = requests.get[URL, stream = True]
    09

    =5

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    92

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    7
     r = requests.get[URL, stream = True]
    8
     r = requests.get[URL, stream = True]
    96# imported the requests library2
     r = requests.get[URL, stream = True]
    98

     r = requests.get[URL, stream = True]
    99image_url7 image_url8image_url9 =0= =2# imported the requests library06=2# imported the requests library08

    # imported the requests library09=8 # imported the requests library11

    # imported the requests library12_______92_______13

    =5

     r = requests.get[URL, stream = True]
    55
     r = requests.get[URL, stream = True]
    75
     r = requests.get[URL, stream = True]
    9# imported the requests library18
     r = requests.get[URL, stream = True]
    78# imported the requests library20

     

    # imported the requests library5

     r = requests.get[URL, stream = True]
    75
     r = requests.get[URL, stream = True]
    9# imported the requests library25_______80_______7

    # imported the requests library5

     r = requests.get[URL, stream = True]
    42

     

     

    =8 # imported the requests library32=___= # imported the requests library35# imported the requests library36

     

    # imported the requests library5# imported the requests library39

    # imported the requests library5

     r = requests.get[URL, stream = True]
    23= # imported the requests library43

     

    # imported the requests library5# imported the requests library46

    # imported the requests library5# imported the requests library48

    # URL of the image to be downloaded is defined as image_url4

     

    # imported the requests library5

    Ưu điểm của việc sử dụng thư viện Yêu cầu để tải xuống tệp web là

    • Người ta có thể dễ dàng tải xuống các thư mục web bằng cách lặp lại đệ quy thông qua trang web
    • Đây là một phương pháp độc lập với trình duyệt và nhanh hơn nhiều
    • Người ta có thể chỉ cần quét một trang web để lấy tất cả các URL của tệp trên một trang web và do đó, tải xuống tất cả các tệp trong một lệnh duy nhất-

      Triển khai Quét web bằng Python với BeautifulSoup

    Blog này được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng write. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới review-team@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác

    Vui lòng viết bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên

    Chủ Đề