我目前正在构建一个Web搜寻器,我想将搜寻器的结果放入MySQL数据库中,以便使用烧瓶显示在HTML页面中。
首先,这是我的整体Python代码。
from selenium import webdriver
from bs4 import BeautifulSoup
import re, csv
import datetime
from flask import Flask, render_template, request, send_file
import pymysql
app = Flask(__name__)
host = '127.0.0.1'
port = 3306
user = 'root'
password = '1111111'
conn = pymysql.connect(host=host, port=port, user=user, passwd=password, db='craw', charset='utf8', autocommit=True)
cursor = conn.cursor()
@app.route('/')
def inputTest():
return render_template('connect.html')
@app.route('/result', methods=['POST','GET'])
def what():
if request.method =="POST":
temp1 = request.form['web']
temp2 = request.form['word']
temp3 = request.form['page']
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(20)
driver.get("https://www.google.com/search?q="+temp1)
base_name ='result'
suffix = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
filename = "_".join([base_name, temp1, temp2, suffix, ".csv"])
for i in range(0, 1):
try:
site = driver.find_element_by_xpath("//cite[@class='iUh30']")
except Exception:
site = driver.find_element_by_xpath("//cite[@class='iUh30 bc']")
except Exception:
site = driver.find_element_by_xpath("//cite[@class='iUh30 rpCHfe']")
clean = re.sub("(http(s)?:\/\/|www.)",'', site.text)
driver.get("https://www.google.com/search?q=" + "site:" + clean + ' ' + temp2)
html0 ="https://www.google.com/search?q=" + "site:" + clean +'%20'+ temp2
with open(filename, 'w', newline='', encoding="utf-8-sig") as A:
writer = csv.writer(A)
writer.writerow(["TITLE", "URL"])
p = 1
while p <= int(temp3):
pager = (p - 1) * 10
html1 = str(html0) +'&start='+ str(pager)
driver.get(html1)
url = driver.page_source
bs0bj = BeautifulSoup(url, "lxml")
for link, name in zip(bs0bj.select('.r > a'), bs0bj.select('.LC20lb > span')):
href = link.get('href')
title = name.get_text()
tt = [title]
ttt = [href]
writer.writerow([title, href])
query = "Into parser (title, href) values (%s, %s)"
cursor.execute(query, (tt, ttt))
p += 1
conn.commit()
cursor.close()
conn.close()
return render_template('connect.html', href = href, title = title)
if __name__=='__main__':
app.debug = True
app.run()
我已经尝试了很多,但是总是在cursor.execute (query, (tt, ttt))
遇到错误。问题是哪一部分?
如何将结果放入MySQL数据库并将该数据库返回到HTML页面?