sqlite3没有插入到选项卡中

2024-04-30 05:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在用PRAW拉reddits的评论说'很多'。我正在尝试将它插入到我正在使用的数据库中

#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评论说很多。但是,如果没有任何错误消息,它不会添加到我的数据库中。数据库名为alotUsers,表名为alot


Tags: andinimport数据库forcommentusernamestorage