2016-06-07 129 views
1

我目前正在学习Python,并且我已经陷入了一个奇怪的问题。Python/MySQL Select返回None与%s

我从一个网站使用beautifulsoup,我已经可以保存到MySQL数据库。现在我想让脚本首先检查抓取的URL是否已经在数据库中。

查询看起来是这样的:

check_url = "SELECT url FROM shop_ig " \ 
    "WHERE url = '%s'" 

在这里我得到的网址:

soup = BeautifulSoup(plain_text, "html.parser") 

for link in soup.findAll('a', {'class': 'ig-preorders-item'}): 
    href = link.get('href') 
    print(href) 

现在,我有网址,我想检查是否href是已经在数据库中所以我执行查询使用:

## Check if url is in db 
    cursor.execute(check_url, href) 
    data = cursor.fetchone() 
    sqlconnect.commit() 

现在奇怪的事情,数据返回None,但是,如果我输入一个永久性的url到查询中,它会在数据库中找到url。

+0

可您打印cursor._executed? – eyalsh

+0

是的,返回b“SELECT URL FROM shop_instantgaming WHERE url ='%s'” 有趣 – Nolibert

+0

'cursor.execute(check_url,(href,))'而不是'cursor.execute(check_url,href)''。 – alecxe

回答

0

感谢alexce我发现了这个问题。

我现在使用:

cursor.execute(check_url, (href,)) 

的选择现在看起来是这样的( '' 删除)

check_url = "SELECT url FROM shop_instantgaming " \ 
"WHERE url = %s"