我正在尝试编写一个带“ fuck”一词的reddit机器人,看看有多少人在reddit上这么说。这是代码:
import praw
import time
import re
import sqlite3
username = "LewisTheRobot"
password = "lewismenelaws"
conn = sqlite3.connect('reddit')
c = conn.cursor()
r = praw.Reddit(user_agent = "Reddit bot")
word_to_match = [r'\bfuck\b', r'\bfucking\b', r'\bfucks\b']
storage = []
r.login(username, password)
def run_bot():
subreddit = r.get_subreddit("all")
print("Grabbing subreddit!")
comments = subreddit.get_comments(limit=200)
print("Grabbing comments!")
for comment in comments:
comment_text = comment.body.lower()
isMatch = any(re.search(string, comment_text) for string in word_to_match)
if comment.id not in storage and isMatch and comment.author not in storage:
print("We have found a fuck boy. Storing username: " + str(comment.author) + "into database.")
storage.append(comment.author)
c.execute("INSERT INTO users (id, Username, subreddit, comment) VALUES(?,?,?,?)", (str(comment.id), str(comment.author), str(comment.subreddit), str(comment.body)))
conn.commit()
print("There are currently " + str(len(storage)) + " fuck boys on reddit at the moment.")
while True:
run_bot()
time.sleep(2)
每当我运行该漫游器时,只要找到匹配项并给我这个错误,它都会崩溃。
据我了解,这意味着数据库中的某些内容不是正确的数据类型,无法插入我的数据库。我很确定这是str(comment.body)
SQL设置的一部分。我的SQLITE数据库具有注释字段作为文本字段。谢谢您的帮助。
这是数据库浏览器中显示的数据库凭据。
IntegrityError
insert语句的结果使用str(comment.id)
as作为ID
字符串,而不是架构要求的整数。
但这是行不通的。如comment.id
您所愿cogqssp
,您将需要更改架构。字段ID
类型为TEXT
。然后您的原始代码将起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句