下午好。我无法弄清楚为什么会抛出错误:
文件“D:/Python/Parser/pars.py”,第 41 行,在 startGrab cursor.execute(query, tmatch) TypeError: not all arguments converted during string formatting
这是代码:
from bs4 import BeautifulSoup
import requests
import psycopg2
conStr = "postgresql://parse:parse@localhost:5432/pars"
conn = psycopg2.connect(conStr)
cursor = conn.cursor()
def startGrab():
base_url = 'http://localhost/way'
url = base_url
try:
page = requests.get(url)
except:
print(url)
soup = BeautifulSoup(page.content, "html5lib")
for row in soup.find_all("tr", {"class" : "belowHeader"}):
i = 0
x = 0
for row2 in row.find_all("td", {"class" : "tdteamname2"}):
if i==0:
team1 = row2.get_text()
else:
team2 = row2.get_text()
i += 1
for row3 in row.find_all("td", {"class" : "tdpercentmw1"}):
if x == 0:
coef1 = row3.get_text()
elif x == 1:
coef2 = row3.get_text()
else:
coef3 = row3.get_text()
x += 1
tmatch = team1+" "+team2+" "+coef1+" "+coef2+" "+coef3
print(tmatch)
query = "INSERT INTO matches(text) VALUES (%s);"
cursor.execute(query, tmatch)
conn.commit()
if __name__ == '__main__':
startGrab()
该方法
excecute
等待第二个参数tuple
。尝试这样写:顺便说一下,文档是这样说的:http: //initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries