-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0233cd7
commit 9386113
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import requests | ||
from bs4 import BeautifulSoup | ||
import os | ||
import uuid | ||
def download_google_images(query, num_images,where): | ||
url = f"https://www.google.com/search?q={query}&source=lnms&hl=en&tbm=isch&asearch=ichunk&async=_id:rg_s,_pms:s,_fmt:pc&sourceid=chrome&ie=UTF-8" | ||
|
||
# Définir l'en-tête de l'utilisateur pour éviter les blocages | ||
headers ={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"} | ||
|
||
|
||
# Récupérer le contenu HTML de la page de recherche Google Images | ||
response = requests.get(url, headers=headers) | ||
soup = BeautifulSoup(response.text, 'html.parser') | ||
|
||
# Créer un dossier pour sauvegarder les images | ||
if not os.path.exists(query): | ||
os.makedirs(query) | ||
print(soup) | ||
#on save en local | ||
with open('test.html', 'w') as f: | ||
f.write(str(soup)) | ||
|
||
# Télécharger les images | ||
image_tags = soup.find_all('a', class_='rg_l') | ||
print(image_tags) | ||
for i, image_tag in enumerate(image_tags[:num_images]): | ||
image_url = image_tag['href'] | ||
#on enleve /imgres?imgurl= | ||
image_url = image_url[15:] | ||
#on cherche & et on degage tout ce qui suit | ||
image_url = image_url.split("&")[0] | ||
|
||
print(image_url) | ||
|
||
|
||
image_data = requests.get(image_url).content | ||
name_file = str(uuid.uuid4()) | ||
with open(f"{where}/image_{name_file}.jpg", 'wb') as f: | ||
f.write(image_data) | ||
print(f"Image {i+1} téléchargée avec succès") | ||
|
||
# Exemple d'utilisation | ||
search_query = input("Entrez votre recherche : ") | ||
num_images_to_download = int(input("Combien d'images voulez-vous télécharger ? ")) | ||
where = input("Ou voulez vous les télécharger ?") | ||
download_google_images(search_query, num_images_to_download,where) |