You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.1 KiB
Python
87 lines
2.1 KiB
Python
# 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 = '''
|
|
<html>
|
|
<head>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="stylesheet" href="pagedjs_files/interface.css">
|
|
<script src="pagedjs_files/paged.polyfill.js"></script>
|
|
<link rel="stylesheet" href="styles/3.css">
|
|
<meta charset="utf-8"/>
|
|
<title>Booklet</title>
|
|
</head>
|
|
<body>
|
|
'''
|
|
|
|
if os.path.isdir('./images/') is True:
|
|
shutil.rmtree('./images/')
|
|
|
|
os.mkdir('./images/')
|
|
|
|
|
|
for q in qq:
|
|
if q == '''<span class="interim"></span>''':
|
|
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'<span class="{qBinded}">{q}</span><br><br>'
|
|
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"""<span class="{qBinded}">{q}</span>"""
|
|
html += f"""<span><img src="./images/{qBinded}.jpg"></span>"""
|
|
|
|
|
|
|
|
html += '''</body>
|
|
</html>'''
|
|
|
|
html = html.replace(' .','.').replace(" '", "'")
|
|
|
|
with open('picindex.html','w') as index:
|
|
index.write(html) |