- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
代码:
import time
import tweepy
import sqlite3
class Listener(tweepy.StreamListener):
conn = sqlite3.connect('/home/daniel/Desktop/activeSites/djeep/djeep.db')
def on_status(self, status):
try:
c = self.conn.cursor()
c.execute("""insert into feed_post values (%r,'%s','%s',%d)""") % (status.id, status.text, status.author.screen_name, status.created_at)
self.conn.commit()
except:
pass
def on_error(self, status_code):
print 'An error has occured! Status code = %s' % status_code
return True # keep stream alive
def on_timeout(self):
print 'timeout...'
def main():
auth = tweepy.OAuthHandler('C_KEY', 'C_SECRET')
auth.set_access_token('ACCESS_TOKEN', 'ACCESS_SECRET')
stream = tweepy.Stream(auth=auth, listener=Listener())
stream.filter(track=('baseball',))
if __name__=="__main__":
try:
main()
except KeyboardInterrupt:
print "See ya!"
我回头一次添加了一行与数据库相关的代码,试图找出是什么破坏了它,似乎是添加了 c.execute()
线。我只是不知道我错过了什么!
最佳答案
数据库的路径应该是脚本的参数,而不是硬编码。它应该在每次实例化类时提供给您的类,而不是在创建类时提供。但是,这并不明显是您问题的原因,还不完全是问题所在:
您的标题表明您无法将任何内容写入您的数据库,但问题正文暗示当您在 c.execute
中添加时某些内容“中断”——这是正确的吗? “坏了”有什么症状?
你的 try\yadda\except\pass 默默地忽略了所有可能的异常 -- 不要那样做! 删除 try\except\pass 只留下 yadda,回答上面的问题,然后让我们知道结果。
更新:您的 c.execute() 语句令人震惊。在不滚动的情况下使其清晰易读,它等同于:
(
c.execute("""insert into feed_post values (%r,'%s','%s',%d)""")
%
(status.id, status.text, status.author.screen_name, status.created_at)
)
换句话说,你有一个右括号严重错位。结果在语法上是有效的,但肯定会在运行时引起异常。
更糟的是:您正在为 SQL 注入(inject)攻击做好准备。使用参数而不是字符串格式:
sql = "insert into feed_post values (?,?,?,?)"
params = (status.id, status.text, status.author.screen_name, status.created_at)
c.execute(sql, params)
这种方法的好处是它应该运行得更快,因为引擎不需要为写入的每一行解析(或让其缓存淹没)通常不同的 SQL 语句。
关于python - 将数据从 Tweepy 提交到 sqlite3 数据库,无论我做什么,数据库都是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8408228/
有人知道如何使用 tweepy 模块中的 search_full_archive 功能吗?我使用了类似于常规搜索的光标功能,但它不起作用。 import tweepy as tw api_key =
有人知道如何使用 tweepy 模块中的 search_full_archive 功能吗?我使用了类似于常规搜索的光标功能,但它不起作用。 import tweepy as tw api_key =
我正在使用此功能从 id 获取所有关注者: def getAllFollowers(id): followers = tweepy.Cursor(api.followers, id = id)
我在 Anacondas Python 3.3 中安装 Tweepy 时遇到问题。 首先,我转到了 Python 3.3 安装的脚本目录。然后我跑了 easy_install tweepy 如果我在默
当我尝试使用 tweepy 进行 Twitter 身份验证时,出现以下错误。 File "/usr/local/lib/python2.7/dist-packages/tweepy/models.
我安装了 pip install tweepy 并且安装没有错误。 Requirement already satisfied: tweepy in /Library/Python/2.7/site-
我在 PyCharm ed 4 中编写了这个 tweepy 源代码。 from __future__ import absolute_import, print_function from tweep
我在尝试在我的代码中导入 tweepy 库时遇到问题,我尝试卸载它然后重新安装它,但我仍然遇到同样的问题。 这是我的代码(错误)和命令行: 谢谢。 最佳答案 Tweepy 还不适用于 python 3
我正在尝试获取用户的 friend 并将他们附加到给定条件的列表中: for friend in tweepy.Cursor(api.friends).items(): if friend n
我正在尝试将 Twitter API 的版本 2 与 tweepy 3.10.0 一起使用,但在遵循文档 https://docs.tweepy.org/en/latest/client.html 时
通过应用 tweepy.Cursor 和 api.search 方法(如下所示),Tweepy 在提取我需要的所有其他信息(主题标签除外)方面做得很好。我从文档中知道 Hashtags 在这个结构状态
TheStreamer.filter(languages=["en"], track=['Bruno Mars is lovely']) 有没有办法让它跟踪一个确切的短语,而不是相关的短语?例如, '
我有以下代码 api = tweepy.API(auth,wait_on_rate_limit=True) for tweet in tweepy.Cursor(api.search,
我最近一直在玩 tweepy,我试图拉动给定用户的关注者和关注者。 followingids = [] followids = [] userid = "Someone"#sets target
我想提取所有国家/地区的所有阿拉伯语推文。 我修改了这个 tutorial中的代码。这是我的搜索查询。api.search(q="*", count=tweetsPerQry, lang ['ar']
我想提取所有国家/地区的所有阿拉伯语推文。 我修改了这个 tutorial中的代码。这是我的搜索查询。api.search(q="*", count=tweetsPerQry, lang ['ar']
当我尝试调用一些方法(例如 show_friendship)时,我收到错误 raise TweepError(error_msg, resp, api_code=api_error_code) twe
我正在使用 tweepy 访问大量推文。许多推文都被截断了,所以我想获取一些推文的全文,我有这些推文的 id。 我的问题是:tweepy api 实例有一种一次下载多条推文的方法 (api.statu
我正在尝试让我的机器人能够通过搜索 API 搜索多个关键字。到目前为止我已经: f = open('swear.txt', 'r') search = f.read().splitlines() f.
我想跟踪特定主题标签的所有推文,但我只需要带有地理位置的推文。此行无法正常工作,结果都是带有地理位置的推文。 stream.filter(track=["hashtag"],locations = G
我是一名优秀的程序员,十分优秀!