写作和阅读peewee

时间:2016-03-10 12:38:44

标签: sqlite python-3.x twitter peewee

我正在编写一个程序来搜索许多人的推文,如果推文的主体是唯一的,它将被存储在该人的sqlite数据库中。我有两个文件,一个用于写入数据库,另一个用于读取数据库并搜索带有搜索词的推文。在写入数据库之前,我在终端上打印了推文,推特正在从推特中正确拉出。当我尝试搜索一个术语时,即使没有术语,所有数据库都没有推文。写入或读取数据库时出现问题。请帮助,我感谢我对python很新。

写作文件:

import requests
import datetime
from bs4 import BeautifulSoup
from peewee import *
from time import sleep

databases = ["femfreq.db", "boris_johnson.db", "barack_obama.db",
         "daily_mail.db", "guardian.db", "times.db", "zac_goldsmith.db",
         "bernie_sanders.db", "george_osborne.db", "john_mcdonnell.db",
         "donald_trump.db", "hillary_clinton.db", "nigel_farage.db"]

urls = ["https://twitter.com/femfreq", "https://twitter.com/BorisJohnson",
    "https://twitter.com/BarackObama",
    "https://twitter.com/MailOnline?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor",
    "https://twitter.com/guardian?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor",
    "https://twitter.com/thetimes",
    "https://twitter.com/ZacGoldsmith?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor",
    "https://twitter.com/berniesanders?lang=en-gb",
    "https://twitter.com/George_Osborne?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor",
    "https://twitter.com/johnmcdonnellMP?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor",
    "https://twitter.com/realDonaldTrump?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor",
    "https://twitter.com/HillaryClinton?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"
    "https://twitter.com/Nigel_Farage?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"]

selection = 0

for database_chosen in databases:

    r = requests.get(urls[selection])
    soup = BeautifulSoup(r.content, "html.parser")
    content =soup.find_all("div",
              {"class":
               "content"})

    db = SqliteDatabase(database_chosen)


    class data_input(Model):
        time_position = DateTimeField(default=datetime.datetime.now)
        header = CharField()
        time_posted = CharField()
        tweet_body = CharField(unique=True)
        class Meta:
            database = db

    db.connect()
    db.create_tables([data_input], safe=True)


    for i in content:
        try:
            data_input.create(header = i.contents[1].text,
                          time_posted = i.contents[3].text,
                          tweet_body = i.contents[5].text)

        except IntegrityError:
            pass

    for i in content:
        print("=============")
        print(i.contents[1].text)
        print(i.contents[3].text)
        print(i.contents[5].text)


    selection += 1

    print("database: {} updated".format(database_chosen))

对于阅读文件

from peewee import *
import datetime

databases = ["femfreq.db", "boris_johnson.db", "barack_obama.db",
         "daily_mail.db", "guardian.db", "times.db", "zac_goldsmith.db",
         "bernie_sanders.db", "george_osborne.db", "john_mcdonnell.db",
         "donald_trump.db", "hillary_clinton.db", "nigel_farage.db"]

search_results = []


search_index = 0
print("")
print("Please enter the number for the database you want to search: ")
for i in databases:
    print("{}:{}".format(i, search_index))
    search_index += 1

select = int(input("please select: "))

database_chosen = databases[select]

db = SqliteDatabase(database_chosen)

class data_input(Model):
    time_position = DateTimeField(default=datetime.datetime.now)
    header = CharField()
    time_posted = CharField()
    tweet_body = CharField(unique=True)
    class Meta:
        database = db

db.connect()


enteries = data_input.select().order_by(data_input.time_position.desc())

print(enteries)

enteries = enteries.where(data_input.tweet_body)
print("")
print("The total number of tweets in {} is: {}".format(database_chosen,
                      len(enteries)))

对于阅读文件,我还没有放入搜索功能,但是当我第一次遇到这个问题时,我会转向它。非常感谢

1 个答案:

答案 0 :(得分:0)

通过在查询中放入“.where(data_input.tweet_body)”来读取条目,您打算做什么?尝试删除整行:

entries = entries.where(data_input.tweet_body)

当你去添加你的搜索时,那时你会想要添加一个where子句......类似于:

entries = entries.where(data_input.tweet_body.contains(search_term))