Good day. I just can not understand why the error flies:

File "D: /Python/Parser/pars.py", line 41, in startGrab cursor.execute (query, tmatch) TypeError: not converted

Here is the code:

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() 

1 answer 1

The excecute method excecute second parameter waits for tuple . Try writing this:

 cursor.execute(query, (tmatch,)) 

In the documentation, by the way, it says: http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries

  • The same error did not help: File "D: /Python/Parser/pars.py", line 41, in startGrab cursor.execute (query, (tmatch))
  • @LeReve, and so: cursor.execute (query, tuple (tmatch)) - Klym
  • one
    @LeReve, you definitely did not forget the comma in the past version: (tmatch,) ? Just the text of the error that you gave the comma is not. - Klym
  • @Klym: (t,) and tuple(t) are different things. For clarity, you can [t] try. - jfs
  • @jfs, I understand that different, just the option with (t, ) should work. - Klym