我正在尝试搜索我的数据库并显示结果,但我的代码没有返回任何内容,而且我不知道错误在哪里。
这就是我抓取数据并将其插入数据库的方式:
def getJobsData(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0'}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, 'html.parser')
data = []
for e in soup.select('.card'):
data.append({
'title': e.h2.text,
'desc': e.ul.get_text('\n', strip=True)
})
try:
for item in data:
c.execute('''INSERT INTO job VALUES(?,?)''',
(item['title'], item['desc']))
return
except:
return
这是搜索代码:
@app.route("/search", methods=['GET', 'POST'])
def search():
error = ""
user_input = None
data = None
try:
if request.method == "POST":
conn = sqlite3.connect('jobs.db')
c = conn.cursor()
c.execute('''SELECT * FROM job WHERE title = (?,)''', (request.form["search"],))
data = c.fetchall()
user_input = (request.form["search"],)
return render_template("search.html", data=data, user_input=user_input)
except Exception as e:
error = (str(e))
return render_template("search.html", error=error, user_input=user_input)
HTML:
<form class="navbar-form navbar-left" action="{{ url_for('search') }}" method="POST" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" name="search" value="{{ request.form.search }}">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
我正在使用 Python、Flask 和 SQLite3。 我做错了什么以及如何解决这个问题?谢谢!
SQL 中的
=
运算符应采用单个值:
c.execute('''SELECT * FROM job WHERE title = ?''', (request.form["search"],))
# Here --------------------------------------^