Я новичок в webscraping и работаю с flask в Python. Я создал страницу поиска с python и flask, я могу сохранить строку, введенную в строке поиска, в переменную python и распечатать ее (просто чтобы убедиться, что она работает). Однако я хочу запустить слово, введенное в python, через веб-страницу, например: https://<url of webpage>/explore?query= + searchWord
, где searchWord
- это текст, введенный пользователем.
Запуск поиска обеспечивает результаты, которые отображаются в div
с id=searchResults'
. Мой вопрос заключается в том, можно ли отобразить весь этот div
на моей flask веб-странице.
Мой python код для моей страницы поиска выглядит следующим образом (и он отображает страницу поиска):
from urllib import request
from bs4 import BeautifulSoup
import requests
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
errors = []
results = {}
if request.method == "POST":
# get word that the user has entered
try:
searchWord = request.form['search']
print(searchWord)
#run a search and display the div code here.
#not sure what to do.
#EDIT:
URL = 'https://visualgenome.org/VGViz/explore?query=' + searchWord
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id='searchResults')
except:
errors.append(
"Unable to get URL. Please make sure it's valid and try again."
)
return render_template('search.html', errors=errors, results=results)
if __name__ == '__main__':
app.run(debug=True)
Это моя search.html
страница:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
font-family: Arial;
}
* {
box-sizing: border-box;
}
form.example input[type=text] {
padding: 10px;
font-size: 17px;
border: 1px solid grey;
float: left;
width: 80%;
background: #f1f1f1;
}
form.example button {
float: left;
width: 20%;
padding: 10px;
background: #2196F3;
color: white;
font-size: 17px;
border: 1px solid grey;
border-left: none;
cursor: pointer;
}
form.example button:hover {
background: #0b7dda;
}
form.example::after {
content: "";
clear: both;
display: table;
}
</style>
</head>
<body>
<h2>Web Service</h2>
<form class="example" method="post" action="/" style="margin:auto;max-width:500px">
<input type="text" placeholder="Search.." name="search2">
<button type="submit" name="submit"><i class="fa fa-search"></i></button>
</form>
<br>
</body>
</html>
РЕДАКТИРОВАТЬ: Это изображение того, как выглядит div searchResults: Структура Div