# Bonus! # Scrape and download images in local from DuckDuckGo # First, you need python. You can download Python from its website: # https://www.python.org/ # Then, you need to install DuckDuckGoImages,open the terminal and digit: # pip3 install DuckDuckGoImages import DuckDuckGoImages as ddg import os import shutil with open('speech.txt','r') as speech: qq = speech.readlines() html = '' html = ''' Booklet ''' if os.path.isdir('./images/') is True: shutil.rmtree('./images/') os.mkdir('./images/') for q in qq: if q == '''''': continue qDDG = q.replace(' ','+') qBinded = q.replace(' ','') print(qDDG) qBinded = qBinded.replace("\n","") os.mkdir(f'./images/{qBinded}') ddg.download(qDDG, folder= f"./images/{qBinded}/", max_urls=10, thumbnails=True) normalize = os.listdir(f"./images/{qBinded}/") print(len(os.listdir(f"./images/{qBinded}/"))) if len(os.listdir(f"./images/{qBinded}/")) == 0: html += f'{q}

' os.rmdir(f'./images/{qBinded}/') continue print('NORMALIZE', normalize) normalize = normalize[0] splitExtension = os.path.splitext(normalize) print('ESTENSIONE', splitExtension) os.rename(f'./images/{qBinded}/{normalize}', f'./images/{qBinded}/{qBinded}') os.replace(f'./images/{qBinded}/{qBinded}', f'./images/{qBinded}.jpg') shutil.rmtree(f'./images/{qBinded}/') html += f"""{q}""" html += f"""""" html += ''' ''' html = html.replace(' .','.').replace(" '", "'") with open('picindex.html','w') as index: index.write(html)