我在搜索数据库(SQLite3)时遇到现有值的问题。我认为问题是搜索值包含连字符。当我在数据库编辑器中使用“”和“'来尝试命令时,它正在工作,但似乎无法在我的代码中使用它。
如果有帮助,我添加了整个代码。不工作的部分是在之后,如果在Arbetsgivare类中re.findall(“[i | I] [t | T] - ”,str(i))。
from bs4 import BeautifulSoup
import requests
import re
import sqlite3
class Connect():
def __init__(self):
global conn
conn = sqlite3.connect('DBJobb.db')
global cursor
cursor = conn.cursor()
print("DB successfully connected.")
try:
conn.execute('''CREATE TABLE Annonser(TITLE TEXT NOT NULL, URL TEXT NOT NULL)''')
print("The DB was created successfully.")
except:
print("The table already exists, no need to create it.")
class Arbetsgivare:
def Polisen():
url = requests.get('http://polisen.se/aktuellt/lediga-jobb')
content = url.content
soup = BeautifulSoup(content)
added = 0
notadded = 0
for i in soup.find_all('a'):
if re.findall("[i|I][t|T]\-", str(i)):
#SELECT both with * and count(*)
conn.execute("SELECT * FROM Annonser WHERE TITLE = ?", (i.get_text(),))
search = cursor.fetchone()
print(search) #Just to see what the value it has.
if search is None: #I have tried with == 0 as well.
conn.execute("INSERT INTO Annonser(TITLE, URL) values (?, ?)", ((i.get_text()), ('http://polisen.se'+i.get('href'))))
conn.commit()
added = added + 1
else:
notadded = notadded + 1
print(str(added)+" entries was added to the DB. There were "+str(notadded)+" entries found which was already in the DB.")
Connect()
Arbetsgivare.Polisen()
答案 0 :(得分:0)
尝试使用引号
SELECT * FROM Annonser WHERE TITLE = 'searchstring'
答案 1 :(得分:0)
好吧,我让它上班了。问题是括号和逗号并使用内容而不是get_text()。这是工作代码。
for i in soup.find_all('a'):
if re.findall("[i|I][t|T]\-", str(i)):
var = str(i.contents)
var = var.replace("[", "").replace("]", "").replace("'", "")
cursor.execute("SELECT * FROM Annonser WHERE TITLE = ?", (var,))
search = cursor.fetchone()
if search is None:
conn.execute("INSERT INTO Annonser(TITLE, URL) values (?, ?)", ((i.get_text()), ('http://polisen.se'+i.get('href'))))
conn.commit()
added = added + 1
else:
notadded = notadded + 1
print(str(added)+" entrie(s) was added from SKL to the DB.")