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.

78 lines
2.3 KiB
Python

3 years ago
# October 2021, copyleft || Kamome and Funix || Speech-to-Derive * The Myth of Natural Language || Roodkapje, Rotterdam
# Scrape images from DuckDuckGo
#
# With duckduckgo_images_api!
3 years ago
from duckduckgo_images_api import search # import the library for scrape
# to create delays :: for having a few seconds to check the console
import time
3 years ago
with open('../speech.txt', 'r') as speech: # let's import the text
# and split it in lines, it will create an array, a list
qq = speech.readlines()
print(qq) # print the array!
time.sleep(2) # check qq in the console!
3 years ago
# declare the first part of the text of the html, we will fill it
# in the process with loops
html = '''
3 years ago
<!DOCTYPE html>
<head>
3 years ago
<meta charset="UTF-8">
<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>
3 years ago
<link rel="stylesheet" href="./styles/2.css">
<title>📡 💻📘</title>
</head>
<body>
3 years ago
<div class="firstP">
<h1 style="position: absolute; top: 0; left:0; color: black;">Title!</h1>
3 years ago
<p style="position: absolute; bottom: 0; right:0;">Authors!</p>
3 years ago
</div>
<div class="contents">
'''
3 years ago
# Elaborate each line :: process every element of the array qq
3 years ago
# q is for "query", qq for "queries", because we will send requests to
# DuckDuckGo searching the text of each line of speech.txt
for q in qq:
print(q) # print the q!
time.sleep(2) # check current q in the console!
3 years ago
q = q.strip()
3 years ago
# remove "\n", which means "return to the next line"
q = q.replace("\n", "")
3 years ago
# Scrape images with search()!
# q is, indeed, the query for DuckDuckGo
results = search(q)
r = results["results"][0]["image"] # get the http link to the image
html += f""" <span> {q} </span>\n""" # Now let's fill the html with text and the pic
html += f""" <img src='{r}'>\n"""
3 years ago
# Close the html text
3 years ago
html += ''' <div>
</body>
</html>'''
3 years ago
html = html.replace(" '", "'")
3 years ago
with open('../2_layout/3.html', 'w') as index: # Save the <html> file!
3 years ago
index.write(html)