Đ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
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
import
8=
requests
0
r = requests.get[URL, stream = True]0
=
requests
4=
requests
6requests
7
r = requests.get[URL, stream = True]7
r = requests.get[URL, stream = True]8
r = requests.get[URL, stream = True]9
image_url
2# imported the requests library
1image_url
4image_url
5# imported the requests library
5image_url
7 image_url
8_______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_url
0
# URL of the image to be downloaded is defined as image_url
2 # URL of the image to be downloaded is defined as image_url
3
# URL of the image to be downloaded is defined as image_url
4
# imported the requests library
5# URL of the image to be downloaded is defined as image_url
6
# imported the requests library
5
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_url
4
# imported the requests library
5
r = requests.get[URL, stream = True]03
# imported the requests library
5
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_url
4
# imported the requests library
5
r = requests.get[URL, stream = True]12
# imported the requests library
5
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 library
5
r = requests.get[URL, stream = True]21
# imported the requests library
5
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]29
image_url
7 r = requests.get[URL, stream = True]31
image_url
9 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 library
5
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_url
2
r = requests.get[URL, stream = True]47
# imported the requests library
5image_url
7
r = requests.get[URL, stream = True]31
image_url
9 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_url
6r = requests.get[URL, stream = True]55
r = requests.get[URL, stream = True]0
=
r = requests.get[URL, stream = True]86
=
requests
6r = 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 library
2r = requests.get[URL, stream = True]98
r = requests.get[URL, stream = True]99
image_url
7 image_url
8image_url
9 =
0=
=
2# imported the requests library
06=
2# imported the requests library
08# imported the requests library
09=
8 # imported the requests library
11
# imported the requests library
12_______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 library
18r = requests.get[URL, stream = True]78
# imported the requests library
20
# imported the requests library
5
r = requests.get[URL, stream = True]75
r = requests.get[URL, stream = True]9
# imported the requests library
25_______80_______7# imported the requests library
5
r = requests.get[URL, stream = True]42
=
8 # imported the requests library
32=
___=
# imported the requests library
35# imported the requests library
36
# imported the requests library
5# imported the requests library
39
# imported the requests library
5
r = requests.get[URL, stream = True]23
=
# imported the requests library
43
# imported the requests library
5# imported the requests library
46
# imported the requests library
5# imported the requests library
48
# URL of the image to be downloaded is defined as image_url
4
# imported the requests library
5
Ư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