Методы получения изображений Васко да Гамы: парсинг веб-страниц и пример API

Чтобы получить изображения Васко да Гамы, вы можете использовать различные методы, включая парсинг веб-страниц, использование API или использование существующих наборов данных. Вот несколько примеров того, как это можно сделать:

Метод 1: парсинг веб-страниц с использованием Python и библиотеки BeautifulSoup

import requests
from bs4 import BeautifulSoup
import urllib
# Define the search query
query = "vasco da gama"
# Encode the query for the URL
encoded_query = urllib.parse.quote(query)
# Construct the search URL
search_url = f"https://www.google.com/search?q={encoded_query}&tbm=isch"
# Send a GET request to the search URL
response = requests.get(search_url)
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, "html.parser")
# Find all image elements
image_elements = soup.find_all("img")
# Extract the image URLs
image_urls = [image["src"] for image in image_elements]
# Print the image URLs
for url in image_urls:
    print(url)

Метод 2. Использование Unsplash API

import requests
# Define the search query
query = "vasco da gama"
# Set up the API endpoint and parameters
url = "https://api.unsplash.com/search/photos"
params = {
    "query": query,
    "client_id": "YOUR_UNSPLASH_ACCESS_KEY"
}
# Send a GET request to the API endpoint
response = requests.get(url, params=params)
# Retrieve the JSON response
data = response.json()
# Extract the image URLs from the response
image_urls = [photo["urls"]["regular"] for photo in data["results"]]
# Print the image URLs
for url in image_urls:
    print(url)

Обратите внимание, что в методе 2 вам необходимо будет зарегистрироваться в Unsplash API и заменить «YOUR_UNSPLASH_ACCESS_KEY» своим фактическим ключом доступа.