import requests
from bs4 import BeautifulSoup
import re
import time
from datetime import datetime


r = requests.get('https://www.google.com/search?q=puillule+png&rlz=1C5CHFA_enFR974FR974&oq=puillule+png&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIJCAEQABgNGIAEMgoIAhAAGAgYDRgeMgoIAxAAGAgYDRgeMgoIBBAAGAgYDRgeMgcIBRAAGO8FMgcIBhAAGO8FMgoIBxAAGIAEGKIEMgoICBAAGIAEGKIEMgoICRAAGIAEGKIE0gEINDkzMWoxajeoAgCwAgA&sourceid=chrome&ie=UTF-8')
print(r)
soup = BeautifulSoup(r.content, 'html.parser')

print(r.content)

images = soup.find_all('img')

for image in images:
    try:
        print(image['src'])
        filename = f"{datetime.now().strftime("%Y%m%d-%H%M%S-%f")}.jpg"
        imageData = requests.get(image['src']).content
        with open(f"images/{filename}", 'wb') as f:
            f.write(imageData)
        
        with open('imgs.html', 'r+') as f:
            raw = f.read()
            f.seek(0)
            soup = BeautifulSoup(raw, 'html.parser')
            main = soup.find('article')
            img = BeautifulSoup(f'<img src="images/{filename}">').img
            main.append(img)
            f.write(soup.prettify())
            f.truncate()
    except:
        print('probleme')

    time.sleep(3)
