Tháng trước, tôi đang làm việc với một dự án yêu cầu tôi tải xuống hàng nghìn hình ảnh từ một trang web. Điều này là không thể đối với tôi để tải xuống thủ công và nó dẫn tôi đến một câu hỏi hay. Làm cách nào tôi có thể tự động hóa nó bằng Python?
Python là một ngôn ngữ đa mục đích và được sử dụng rộng rãi để viết kịch bản. Chúng ta có thể viết các tập lệnh Python để tự động hóa mọi thứ hàng ngày. Giả sử chúng tôi muốn tải xuống hình ảnh google với nhiều truy vấn tìm kiếm. Thay vì thực hiện thủ công, chúng tôi có thể tự động hóa quy trình. Cách cài đặt Mô-đun cần thiết.
pip install google_images_download
Hãy xem cách viết tập lệnh Python để tải xuống hình ảnh Google bằng Python bằng mô-đun google_images_download. Dưới đây là mã Python.
Quét web là một kỹ thuật để lấy dữ liệu từ các trang web. Khi lướt web, nhiều trang web không cho phép người dùng lưu dữ liệu cho mục đích cá nhân. Một cách là sao chép-dán dữ liệu theo cách thủ công, vừa tẻ nhạt vừa tốn thời gian. Web Scraping là quá trình tự động hóa quá trình trích xuất dữ liệu từ các trang web. Trong bài viết này, chúng tôi sẽ thảo luận về cách chúng tôi có thể tải xuống tất cả hình ảnh từ một trang web bằng python
Mô-đun cần thiết
- bs4. Beautiful Soup[bs4] là một thư viện Python để lấy dữ liệu ra khỏi các tệp HTML và XML. Mô-đun này không được tích hợp sẵn với Python
- yêu cầu. Yêu cầu cho phép bạn gửi HTTP/1. 1 yêu cầu cực kỳ dễ dàng. Mô-đun này cũng không tích hợp sẵn với Python
- hệ điều hành. Mô-đun hệ điều hành trong python cung cấp các chức năng để tương tác với hệ điều hành. Hệ điều hành, đi kèm với các mô-đun tiện ích tiêu chuẩn của Python. Mô-đun này cung cấp một cách di động để sử dụng chức năng phụ thuộc vào hệ điều hành
Tiếp cận
- Nhập mô-đun
- Nhận mã HTML
- Nhận danh sách các thẻ img từ Mã HTML bằng phương thức findAll trong Beautiful Soup
images = soup.findAll['img']
Tạo thư mục riêng để tải xuống hình ảnh bằng phương pháp mkdir trong os
os.mkdir[folder_name]
- Lặp lại tất cả các hình ảnh và lấy URL nguồn của hình ảnh đó
- Sau khi nhận được URL nguồn, bước cuối cùng là tải xuống hình ảnh
- Tìm nạp nội dung của hình ảnh
r = requests.get[Source URL].content
- Tải xuống hình ảnh bằng Xử lý tệp
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]
Chương trình
Python3
images = soup.findAll['img']89
images = soup.findAll['img']90
images = soup.findAll['img']91
images = soup.findAll['img']92
images = soup.findAll['img']91
images = soup.findAll['img']94
images = soup.findAll['img']91
os.mkdir[folder_name]1
os.mkdir[folder_name]2
os.mkdir[folder_name]3
os.mkdir[folder_name]4
os.mkdir[folder_name]5
os.mkdir[folder_name]6
os.mkdir[folder_name]7
os.mkdir[folder_name]8
os.mkdir[folder_name]9
r = requests.get[Source URL].content0
r = requests.get[Source URL].content1
r = requests.get[Source URL].content2
r = requests.get[Source URL].content3
r = requests.get[Source URL].content4
os.mkdir[folder_name]8
r = requests.get[Source URL].content6
os.mkdir[folder_name]8
r = requests.get[Source URL].content8
os.mkdir[folder_name]5
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]0
os.mkdir[folder_name]5
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]2____27
os.mkdir[folder_name]8
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]5
r = requests.get[Source URL].content2____47
r = requests.get[Source URL].content4
os.mkdir[folder_name]8
images = soup.findAll['img']890
os.mkdir[folder_name]5
images = soup.findAll['img']892
os.mkdir[folder_name]5
images = soup.findAll['img']894
images = soup.findAll['img']895
os.mkdir[folder_name]3
images = soup.findAll['img']897
images = soup.findAll['img']898
os.mkdir[folder_name]5
images = soup.findAll['img']900
os.mkdir[folder_name]5
images = soup.findAll['img']902
r = requests.get[Source URL].content0
images = soup.findAll['img']904
os.mkdir[folder_name]5
images = soup.findAll['img']906
os.mkdir[folder_name]5
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]5
images = soup.findAll['img']909
images = soup.findAll['img']910
r = requests.get[Source URL].content4
os.mkdir[folder_name]5
images = soup.findAll['img']913
os.mkdir[folder_name]5
images = soup.findAll['img']915
images = soup.findAll['img']916______1917
r = requests.get[Source URL].content0
images = soup.findAll['img']904
os.mkdir[folder_name]7
os.mkdir[folder_name]8
images = soup.findAll['img']922
images = soup.findAll['img']923
images = soup.findAll['img']924
images = soup.findAll['img']925
images = soup.findAll['img']926
images = soup.findAll['img']927
images = soup.findAll['img']928
images = soup.findAll['img']929
images = soup.findAll['img']910
images = soup.findAll['img']929
images = soup.findAll['img']912
images = soup.findAll['img']929
images = soup.findAll['img']914
images = soup.findAll['img']929
images = soup.findAll['img']916
images = soup.findAll['img']927
images = soup.findAll['img']918
images = soup.findAll['img']927
images = soup.findAll['img']940
images = soup.findAll['img']927
os.mkdir[folder_name]6______27
images = soup.findAll['img']944
images = soup.findAll['img']945
images = soup.findAll['img']944
images = soup.findAll['img']947
r = requests.get[Source URL].content0
images = soup.findAll['img']949
os.mkdir[folder_name]00
os.mkdir[folder_name]01
images = soup.findAll['img']944
images = soup.findAll['img']927
os.mkdir[folder_name]04
images = soup.findAll['img']927
os.mkdir[folder_name]06
images = soup.findAll['img']927
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]2
os.mkdir[folder_name]7
images = soup.findAll['img']944
os.mkdir[folder_name]6
os.mkdir[folder_name]7
os.mkdir[folder_name]13
os.mkdir[folder_name]14
os.mkdir[folder_name]13
images = soup.findAll['img']947
r = requests.get[Source URL].content0
images = soup.findAll['img']949
os.mkdir[folder_name]19
os.mkdir[folder_name]01
images = soup.findAll['img']944
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]2
os.mkdir[folder_name]7
os.mkdir[folder_name]13
os.mkdir[folder_name]6
os.mkdir[folder_name]7
images = soup.findAll['img']929
os.mkdir[folder_name]28
images = soup.findAll['img']929
images = soup.findAll['img']947
r = requests.get[Source URL].content0
images = soup.findAll['img']949
os.mkdir[folder_name]33
os.mkdir[folder_name]01
os.mkdir[folder_name]13____42
os.mkdir[folder_name]7
images = soup.findAll['img']929
os.mkdir[folder_name]6____27
os.mkdir[folder_name]41
os.mkdir[folder_name]42
os.mkdir[folder_name]41
images = soup.findAll['img']947
r = requests.get[Source URL].content0
images = soup.findAll['img']949
os.mkdir[folder_name]47
os.mkdir[folder_name]01
images = soup.findAll['img']929
os.mkdir[folder_name]50
images = soup.findAll['img']929____42____27
os.mkdir[folder_name]41
os.mkdir[folder_name]55
images = soup.findAll['img']927
os.mkdir[folder_name]57
images = soup.findAll['img']927
os.mkdir[folder_name]59
images = soup.findAll['img']927
os.mkdir[folder_name]6______27
images = soup.findAll['img']944
os.mkdir[folder_name]64
r = requests.get[Source URL].content0
os.mkdir[folder_name]66
images = soup.findAll['img']944
os.mkdir[folder_name]6
os.mkdir[folder_name]7
os.mkdir[folder_name]13
os.mkdir[folder_name]71
os.mkdir[folder_name]13
os.mkdir[folder_name]64
r = requests.get[Source URL].content0
os.mkdir[folder_name]75
os.mkdir[folder_name]76
os.mkdir[folder_name]77
r = requests.get[Source URL].content4
images = soup.findAll['img']944
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]2
os.mkdir[folder_name]81
os.mkdir[folder_name]13
os.mkdir[folder_name]83
os.mkdir[folder_name]13
os.mkdir[folder_name]85______286
images = soup.findAll['img']909
os.mkdir[folder_name]88
os.mkdir[folder_name]89
os.mkdir[folder_name]90
os.mkdir[folder_name]91
images = soup.findAll['img']929
os.mkdir[folder_name]93
os.mkdir[folder_name]13
os.mkdir[folder_name]95
os.mkdir[folder_name]13
images = soup.findAll['img']902
os.mkdir[folder_name]98
r = requests.get[Source URL].content0
r = requests.get[Source URL].content00
images = soup.findAll['img']927
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]2
os.mkdir[folder_name]7
images = soup.findAll['img']944
os.mkdir[folder_name]55
os.mkdir[folder_name]8
r = requests.get[Source URL].content07
os.mkdir[folder_name]8
r = requests.get[Source URL].content09
os.mkdir[folder_name]8
r = requests.get[Source URL].content11
os.mkdir[folder_name]8
images = soup.findAll['img']915
images = soup.findAll['img']902____30
r = requests.get[Source URL].content0
images = soup.findAll['img']916
images = soup.findAll['img']926
images = soup.findAll['img']927
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]5______32
r = requests.get[Source URL].content22
r = requests.get[Source URL].content4
images = soup.findAll['img']927
os.mkdir[folder_name]8
r = requests.get[Source URL].content26
os.mkdir[folder_name]8
r = requests.get[Source URL].content28
os.mkdir[folder_name]7
images = soup.findAll['img']927
# Enter File Name with Extension like jpg, png etc.. with open["File Name","wb+"] as f: f.write[r]5____1909
r = requests.get[Source URL].content33
r = requests.get[Source URL].content4
r = requests.get[Source URL].content35
os.mkdir[folder_name]3
r = requests.get[Source URL].content37
images = soup.findAll['img']898
os.mkdir[folder_name]5
r = requests.get[Source URL].content40
os.mkdir[folder_name]5
os.mkdir[folder_name]64
r = requests.get[Source URL].content0
r = requests.get[Source URL].content44
os.mkdir[folder_name]5
r = requests.get[Source URL].content46
os.mkdir[folder_name]5
r = requests.get[Source URL].content48
r = requests.get[Source URL].content0
r = requests.get[Source URL].content50
r = requests.get[Source URL].content51
r = requests.get[Source URL].content4
os.mkdir[folder_name]5
r = requests.get[Source URL].content54
os.mkdir[folder_name]5
r = requests.get[Source URL].content56
r = requests.get[Source URL].content0
r = requests.get[Source URL].content58
r = requests.get[Source URL].content59
r = requests.get[Source URL].content4
os.mkdir[folder_name]5
r = requests.get[Source URL].content62
os.mkdir[folder_name]5
r = requests.get[Source URL].content64
r = requests.get[Source URL].content65
r = requests.get[Source URL].content66
r = requests.get[Source URL].content0
r = requests.get[Source URL].content1______32
r = requests.get[Source URL].content70
r = requests.get[Source URL].content4
r = requests.get[Source URL].content72
r = requests.get[Source URL].content73
đầu ra
https. //phương tiện truyền thông. chuyên viên máy tính. org/wp-content/uploads/20201211190257/FreeOnlineScreenRecorderProject1. mp4