Lưu nhiều hình ảnh python

Tự động tải xuống hình ảnh từ một số trang HTML của bạn là một kỹ năng cần thiết, trong hướng dẫn này, bạn sẽ học 4 phương pháp về cách tải xuống hình ảnh bằng Python

Hãy bắt đầu với ví dụ đơn giản nhất, nếu chúng ta đã có danh sách các URL hình ảnh thì chúng ta có thể làm theo quy trình này

  1. Thay đổi thành một thư mục nơi chúng tôi muốn lưu trữ tất cả các hình ảnh
  2. Yêu cầu tải xuống tất cả các hình ảnh, từng cái một
  3. Chúng tôi cũng sẽ bao gồm xử lý lỗi để nếu một URL không còn tồn tại thì mã vẫn hoạt động

Nhập khẩu Python

!pip install tldextract
____1
import requests
import os
import subprocess
import urllib.request
from bs4 import BeautifulSoup
import tldextract
!mkdir all_images
!ls

Thay đổi vào thư mục của thư mục có tên all_images, điều này có thể được thực hiện bởi một trong hai

# !cd all_images
# os.chdir['path']
os.chdir['all_images']
!pwd
/Users/jamesaphoenix/Desktop/Imran_And_James/Python_For_SEO/6_downloading_multiple_images/all_images

phương pháp một. Cách tải xuống nhiều hình ảnh từ danh sách Python

Để tải xuống nhiều hình ảnh, chúng tôi sẽ sử dụng thư viện yêu cầu. Chúng tôi cũng sẽ tạo một danh sách python để lưu trữ bất kỳ URL hình ảnh bị hỏng nào không trả về mã trạng thái 200

broken_images = []
____9
Requirement already satisfied: tldextract in /opt/anaconda3/lib/python3.7/site-packages [2.2.2]
Requirement already satisfied: requests>=2.1.0 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.22.0]
Requirement already satisfied: setuptools in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [46.0.0.post20200309]
Requirement already satisfied: requests-file>=1.4 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [1.5.1]
Requirement already satisfied: idna in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.8]
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,=1.21.1 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [1.25.8]
Requirement already satisfied: chardet=3.0.2 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [3.0.4]
Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [2019.11.28]
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages [from requests-file>=1.4->tldextract] [1.14.0]
0
Requirement already satisfied: tldextract in /opt/anaconda3/lib/python3.7/site-packages [2.2.2]
Requirement already satisfied: requests>=2.1.0 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.22.0]
Requirement already satisfied: setuptools in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [46.0.0.post20200309]
Requirement already satisfied: requests-file>=1.4 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [1.5.1]
Requirement already satisfied: idna in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.8]
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,=1.21.1 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [1.25.8]
Requirement already satisfied: chardet=3.0.2 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [3.0.4]
Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [2019.11.28]
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages [from requests-file>=1.4->tldextract] [1.14.0]
1

☝️ Thấy sao mà đơn giản thế. ☝️

Nếu bạn kiểm tra thư mục của mình, bây giờ bạn sẽ tải xuống tất cả các hình ảnh có chứa mã trạng thái 200

Phương pháp hai. Cách tải xuống nhiều hình ảnh từ nhiều trang web HTML

Nếu chúng tôi chưa có URL hình ảnh chính xác, chúng tôi sẽ cần thực hiện các thao tác sau

  1. Tải xuống nội dung HTML của mọi trang web
  2. Trích xuất tất cả các URL hình ảnh cho mỗi trang
  3. Tạo tên tập tin
  4. Kiểm tra xem mã trạng thái hình ảnh có phải là 200 không
  5. Viết tất cả các hình ảnh vào máy tính cục bộ của bạn

Trang web này internetingishard. com có ​​một số URL hình ảnh tương đối. Do đó, chúng tôi sẽ cần đảm bảo rằng mã của chúng tôi có thể xử lý hai loại URL nguồn hình ảnh sau

  • Filepath chính xác. https. //www. internetingishard. com/html-and-css/links-and-images/html-attributes-6f5690. png
  • Filepath tương đối. /html-and-css/links-and-images/html-attributes-6f5690. png
Requirement already satisfied: tldextract in /opt/anaconda3/lib/python3.7/site-packages [2.2.2]
Requirement already satisfied: requests>=2.1.0 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.22.0]
Requirement already satisfied: setuptools in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [46.0.0.post20200309]
Requirement already satisfied: requests-file>=1.4 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [1.5.1]
Requirement already satisfied: idna in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.8]
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,=1.21.1 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [1.25.8]
Requirement already satisfied: chardet=3.0.2 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [3.0.4]
Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [2019.11.28]
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages [from requests-file>=1.4->tldextract] [1.14.0]
2

Chúng tôi cũng sẽ trích xuất tên miền của mọi URL trong khi chúng tôi duyệt qua các trang web như vậy

Requirement already satisfied: tldextract in /opt/anaconda3/lib/python3.7/site-packages [2.2.2]
Requirement already satisfied: requests>=2.1.0 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.22.0]
Requirement already satisfied: setuptools in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [46.0.0.post20200309]
Requirement already satisfied: requests-file>=1.4 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [1.5.1]
Requirement already satisfied: idna in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.8]
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,=1.21.1 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [1.25.8]
Requirement already satisfied: chardet=3.0.2 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [3.0.4]
Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [2019.11.28]
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages [from requests-file>=1.4->tldextract] [1.14.0]
3____14
Requirement already satisfied: tldextract in /opt/anaconda3/lib/python3.7/site-packages [2.2.2]
Requirement already satisfied: requests>=2.1.0 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.22.0]
Requirement already satisfied: setuptools in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [46.0.0.post20200309]
Requirement already satisfied: requests-file>=1.4 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [1.5.1]
Requirement already satisfied: idna in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.8]
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,=1.21.1 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [1.25.8]
Requirement already satisfied: chardet=3.0.2 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [3.0.4]
Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [2019.11.28]
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages [from requests-file>=1.4->tldextract] [1.14.0]
5
Requirement already satisfied: tldextract in /opt/anaconda3/lib/python3.7/site-packages [2.2.2]
Requirement already satisfied: requests>=2.1.0 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.22.0]
Requirement already satisfied: setuptools in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [46.0.0.post20200309]
Requirement already satisfied: requests-file>=1.4 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [1.5.1]
Requirement already satisfied: idna in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.8]
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,=1.21.1 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [1.25.8]
Requirement already satisfied: chardet=3.0.2 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [3.0.4]
Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [2019.11.28]
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages [from requests-file>=1.4->tldextract] [1.14.0]
6

Bây giờ, hãy kiểm tra lại và lọc từ điển của chúng tôi để chúng tôi chỉ xem các trang web có ít nhất 1 thẻ hình ảnh

Requirement already satisfied: tldextract in /opt/anaconda3/lib/python3.7/site-packages [2.2.2]
Requirement already satisfied: requests>=2.1.0 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.22.0]
Requirement already satisfied: setuptools in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [46.0.0.post20200309]
Requirement already satisfied: requests-file>=1.4 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [1.5.1]
Requirement already satisfied: idna in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.8]
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,=1.21.1 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [1.25.8]
Requirement already satisfied: chardet=3.0.2 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [3.0.4]
Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [2019.11.28]
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages [from requests-file>=1.4->tldextract] [1.14.0]
7____18

Một cách dễ dàng hơn để viết đoạn mã trên là thông qua việc hiểu từ điển

Requirement already satisfied: tldextract in /opt/anaconda3/lib/python3.7/site-packages [2.2.2]
Requirement already satisfied: requests>=2.1.0 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.22.0]
Requirement already satisfied: setuptools in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [46.0.0.post20200309]
Requirement already satisfied: requests-file>=1.4 in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [1.5.1]
Requirement already satisfied: idna in /opt/anaconda3/lib/python3.7/site-packages [from tldextract] [2.8]
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,=1.21.1 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [1.25.8]
Requirement already satisfied: chardet=3.0.2 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [3.0.4]
Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/lib/python3.7/site-packages [from requests>=2.1.0->tldextract] [2019.11.28]
Requirement already satisfied: six in /opt/anaconda3/lib/python3.7/site-packages [from requests-file>=1.4->tldextract] [1.14.0]
9

Giờ đây, chúng tôi có thể xóa tất cả các URL hình ảnh bên trong mọi khóa từ điển và thay đổi tất cả các đường dẫn URL tương đối thành các đường dẫn URL chính xác

Hãy bắt đầu bằng cách in ra tất cả các nguồn hình ảnh khác nhau để xem chúng ta có thể cần làm sạch dữ liệu bên dưới như thế nào

import requests
import os
import subprocess
import urllib.request
from bs4 import BeautifulSoup
import tldextract
0

Đối với phạm vi của hướng dẫn này, tôi đã quyết định

  • Xóa các liên kết logo bằng //
  • Thêm tên miền vào các URL tương đối
import requests
import os
import subprocess
import urllib.request
from bs4 import BeautifulSoup
import tldextract
1
import requests
import os
import subprocess
import urllib.request
from bs4 import BeautifulSoup
import tldextract
2____23

Sau khi làm sạch các URL hình ảnh, bây giờ chúng ta có thể tham khảo phương pháp một để tải hình ảnh về máy tính của mình

Lần này hãy chuyển đổi nó thành một chức năng

import requests
import os
import subprocess
import urllib.request
from bs4 import BeautifulSoup
import tldextract
4
!pwd
/Users/jamesaphoenix/Desktop/Imran_And_James/Python_For_SEO/6_downloading_multiple_images/all_images
import requests
import os
import subprocess
import urllib.request
from bs4 import BeautifulSoup
import tldextract
6

Tuyệt vời

Bây giờ có một số điều mà chúng tôi không nhất thiết phải bảo hiểm bao gồm

  • http. // chỉ các url hình ảnh
  • http. //www. chỉ url hình ảnh

Nhưng phần lớn, bạn sẽ có thể tải xuống hàng loạt hình ảnh

Cách tăng tốc độ tải xuống hình ảnh của bạn

Khi làm việc với 100 hoặc 1000 URL, điều quan trọng là tránh sử dụng phương pháp đồng bộ để tải xuống hình ảnh. Cách tiếp cận không đồng bộ có nghĩa là chúng tôi có thể tải xuống nhiều trang web hoặc nhiều hình ảnh song song

Điều này có nghĩa là thời gian thực hiện tổng thể sẽ nhanh hơn nhiều

ThreadPoolExecutor[]

ThreadPoolExecutor là một trong các gói I/O tích hợp sẵn của python để tạo hành vi không đồng bộ thông qua nhiều luồng. Để sử dụng nó, chúng tôi sẽ đảm bảo rằng chức năng sẽ chỉ hoạt động trên một URL duy nhất

Sau đó, chúng tôi sẽ chuyển danh sách URL hình ảnh cho nhiều công nhân 😉

import requests
import os
import subprocess
import urllib.request
from bs4 import BeautifulSoup
import tldextract
7____28
import requests
import os
import subprocess
import urllib.request
from bs4 import BeautifulSoup
import tldextract
9

Đoạn mã dưới đây sẽ tạo một thư mục mới và sau đó đặt nó làm thư mục hoạt động hiện tại

!mkdir all_images
0
!mkdir all_images
1

Bạn nên tải xuống hình ảnh nhưng với tốc độ nhanh hơn nhiều

Lập trình không đồng bộ

Cũng giống như JavaScript, Python 3. 6+ đi kèm với hỗ trợ riêng cho các đồng quy trình được gọi là asyncio. Tương tự như NodeJS, có một phương pháp dành cho bạn để tạo các vòng lặp sự kiện tùy chỉnh cho mã không đồng bộ

Chúng tôi cũng sẽ cần tải xuống thư viện yêu cầu HTTP mã không đồng bộ có tên là aiohttp

!mkdir all_images
2

Chúng tôi cũng sẽ tải xuống aiofiles cho phép chúng tôi ghi nhiều tệp hình ảnh không đồng bộ

!mkdir all_images
3____34
!mkdir all_images
5

Cách tải xuống 1 tệp không đồng bộ

!mkdir all_images
6
!mkdir all_images
7____38
!mkdir all_images
9

Chúng tôi sẽ cần cấu trúc mã hơi khác một chút để phiên bản không đồng bộ hoạt động trên nhiều tệp

  1. Chúng tôi sẽ có chức năng tìm nạp để truy vấn mọi URL hình ảnh
  2. Chúng ta sẽ có một hàm main tạo, sau đó thực thi một loạt các đồng quy trình
!ls
0
!ls
1
!ls
2

☝️☝️☝️ Lưu ý khi chúng ta gọi hàm này, nó không thực sự chạy và tạo ra một đồng quy trình. ☝️☝️☝️

Sau đó, chúng tôi có thể sử dụng asyncio làm phương thức để thực hiện tất cả các lệnh gọi tìm nạp cần được hoàn thành

Nếu bạn gặp loại lỗi này khi chạy lệnh sau

!ls
3

Có khả năng là do bạn đang cố chạy asyncio trong một vòng lặp sự kiện, điều này không thể thực hiện được. [Máy tính xách tay Jupyter chạy trong một vòng lặp sự kiện. ]

Theo thiết kế, asyncio không cho phép vòng lặp sự kiện của nó được lồng vào nhau. Điều này đặt ra một vấn đề thực tế. Khi ở trong môi trường nơi vòng lặp sự kiện đang chạy, không thể chạy tác vụ và chờ kết quả. Cố gắng làm như vậy sẽ báo lỗi “RuntimeError. Vòng lặp sự kiện này đang chạy”

Do đó, bạn sẽ cần sử dụng thư viện Nest-asyncio khi sử dụng Jupyter Notebook

!ls
4

Cách tải xuống nhiều tệp Python bên trong tệp Python [. py]

Hãy lưu biến chứa các URL của chúng ta vào một. tập tin txt

!ls
5

Tạo một tệp Python

Sau đó, bạn sẽ cần tạo một tệp python và thêm đoạn mã sau vào đó

!ls
6

Sau đó chạy tập lệnh python trong thiết bị đầu cuối/dòng lệnh của bạn với

!ls
7

Hãy chia nhỏ những gì đang xảy ra trong đoạn mã trên

  1. Chúng tôi đang nhập tất cả các gói có liên quan để lập trình không đồng bộ với các tệp
  2. Sau đó, chúng tôi tạo một thư mục mới
  3. Sau khi tạo thư mục mới, chúng tôi thay đổi thư mục đó thành thư mục làm việc đang hoạt động
  4. Sau đó, chúng tôi đọc dữ liệu biến đã được lưu trước đó từ tệp có tên là hình ảnh. txt
  5. Sau đó, chúng tôi tạo một loạt các đồng quy trình và thực thi chúng trong hàm main[] với asyncio
  6. Khi các đồng quy trình này được thực thi, mọi tệp được lưu không đồng bộ vào máy tính của bạn

Cuối cùng, hãy dọn dẹp và xóa tất cả các thư mục để làm sạch môi trường của chúng ta

_______48____49____50

Khả năng tải xuống hình ảnh bằng python cho phép bạn mở rộng khả năng tự động hóa của mình và những chương trình, API khác, v.v. mà bạn có thể sử dụng dữ liệu hình ảnh đó với

Chủ Đề