【发布时间】:2019-04-09 17:25:39
【问题描述】:
我已经抓取了一些网站并将 html 信息存储在 sqlite 数据库中。现在,我想提取和存储电子邮件地址。我能够成功提取并打印 id 和电子邮件。但是,当我尝试使用这些新的电子邮件地址更新数据库时,我不断收到 TypeError: "'NoneType' object is not subscriptable" 和 "sqlite3.InterfaceError: Error binding parameter 0 - possible unsupported type"。
我已验证我在更新语句中使用的数据类型与我的数据库相同(id 是 int 类,email 是 str)。我在谷歌上搜索了一堆不同的例子,并且对语法进行了很多研究。
我还尝试删除更新语句中的 Where 子句,但得到了相同的错误。
import sqlite3
import re
conn = sqlite3.connect('spider.sqlite')
cur = conn.cursor()
x = cur.execute('SELECT id, html FROM Pages WHERE html is NOT NULL and email is NULL ORDER BY RANDOM()').fetchone()
#print(x)#for testing purposes
for row in x:
row = cur.fetchone()
id = row[0]
html = row[1]
email = re.findall(b'[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+', html)
#print(email)#testing purposes
if not email:
email = 'no email found'
print(id, email)
cur.execute('''UPDATE pages SET email = ? WHERE id = ? ''', (email, id))
conn.commit
我希望更新语句使用为相应行提取的电子邮件地址更新数据库。
【问题讨论】:
-
请在问题正文中包含异常的完整回溯。此外,您能否将数据缩减为产生问题的单行并提供
id和html值?
标签: python-3.x sqlite