【发布时间】:2015-01-22 19:59:24
【问题描述】:
我一直在使用 PRAW 来拉取说“很多”的 reddits cmets。我正在尝试将它插入到我正在使用的数据库中。
#importing praw for reddit api and time to make intervals
import praw
import time
import re
import sqlite3
username = "LewisTheRobot"
password = ""
conn = sqlite3.connect('alotUsers')
c = conn.cursor()
r = praw.Reddit(user_agent = "Counts people who say alot")
word_to_match = [r'\balot\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("Match found! Storing username: " + str(comment.author) + " into list.")
storage.append(comment.author)
c.execute("INSERT INTO alot (id, username) VALUES(?, ?)", (str(comment.id), str(comment.author)))
conn.commit
print("There are currently: " + str(len(storage)) + " people who use 'alot' instead of ' a lot'.")
while True:
run_bot()
time.sleep(5)
目前它会添加到列表中并找到说很多的 reddit cmets。但是,没有任何错误消息,它不会添加到我的数据库中。数据库名称是 alotUsers,表是 alot。
【问题讨论】:
-
conn.commit是一个函数。您检索它,但从不调用它。请改用conn.commit()。