【问题标题】:Trying to capture data from twitter hashtag feeds using tweepy尝试使用 tweepy 从 twitter 主题标签提要中捕获数据
【发布时间】:2015-08-14 07:56:58
【问题描述】:

运行我的服务器时出现以下错误

ValueError at /
The view hashtrack.views.index didn't return an HttpResponse object. It returned None instead.
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.8.2
Exception Type: ValueError
Exception Value:    
The view hashtrack.views.index didn't return an HttpResponse object. It returned None instead.
Exception Location: C:\Python34\lib\site-packages\django\core\handlers\base.py in get_response, line 151
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.3
Python Path:    
['C:\\Users\\torjeli\\tweesh',
 'C:\\Python34\\lib\\site-packages\\setuptools-15.2-py3.4.egg',
 'C:\\Python34\\lib\\site-packages\\distribute-0.6.49-py3.4.egg',
 'C:\\Python34\\lib\\site-packages\\cython-0.22-py3.4-win32.egg',
 'C:\\Python34\\lib\\site-packages\\scrapy-0.24.6-py3.4.egg',
 'C:\\Python34\\lib\\site-packages\\six-1.9.0-py3.4.egg',
 'C:\\Python34\\lib\\site-packages\\cssselect-0.9.1-py3.4.egg',
 'C:\\Python34\\lib\\site-packages\\pyopenssl-0.15.1-py3.4.egg',
 'C:\\Windows\\system32\\python34.zip',
 'C:\\Python34\\DLLs',
 'C:\\Python34\\lib',
 'C:\\Python34',
 'C:\\Python34\\lib\\site-packages',
 'C:\\Python34\\lib\\site-packages\\win32',
 'C:\\Python34\\lib\\site-packages\\win32\\lib',
 'C:\\Python34\\lib\\site-packages\\Pythonwin']
Server time:    Sun, 31 May 2015 18:59:22 -0400

我尝试在页面背景上运行脚本,但我得到相同的错误代码,主题标签确实打印在控制台窗口中,我试图将推文放入 csv 文件以供以后使用数据。

from django.shortcuts import render_to_response
from django.http import HttpResponse
from tweepy import *


CKey = 'xxxxx'           #API Key
ConSec = 'xxxxx'         #Consumer Secret
AccTok = 'xxxxx'         #Access Token
AccTSec = 'xxxxx'        #Access Token Secret


class StdOutListener(StreamListener):

    def on_status(self, status):

        print(status.text)
        return True

    def on_error(self, status):

        print(status)


def index(request):
    try:
        listener = StdOutListener()
        auth = OAuthHandler(CKey, ConSec)
        auth.set_access_token(AccTok, AccTSec)
        stream = Stream(auth, listener)
        gimme = stream.filter(track=["lol"])
        return HttpResponse(gimme)

        #return render_to_response('index.html')
    except RuntimeError:
        pass

【问题讨论】:

  • stream.filter(track=["lol"]) 输出是什么?
  • 监控推特上所有匹配 LOL (#LOL) 的标签
  • 您有没有想过出现异常是有原因的?仅仅为了“通过”而抓住它们必然会导致问题。
  • 我知道,我这样做是为了看看我还能得到什么其他错误,如您所见,第一个是 RunTimeError
  • 你列出的第一个实际上是:The view hashtrack.views.index didn't return an HttpResponse object. It returned None instead. 所以检查你的视图,因为它在某些设置中没有返回 HttpResponse,它返回 None。

标签: python django python-3.x twitter django-views


【解决方案1】:

所以,我设法在 CSV 上捕获数据,但在进程运行时页面没有加载......但我确实得到了一个包含我想要的所有信息的 CSV 文件。

from django.http import HttpResponse
from tweepy import *



CKey = 'xxxxx'           #API Key
ConSec = 'xxxxx'         #Consumer Secret
AccTok = 'xxxxx'         #Access Token
AccTSec = 'xxxxx'        #Access Token Secret


class StdOutListener(StreamListener):

def on_status(self, status):
    try:

        hashdb = open("hashtag.csv", 'a')
        hashdb.write(status.text)
        hashdb.write('\n')
        hashdb.close()
    except BaseException:
        print("could not process")
def on_error(self, status_code):
    print(status_code)
    return status_code


def index(request):

    listener = StdOutListener()
    auth = OAuthHandler(CKey, ConSec)
    auth.set_access_token(AccTok, AccTSec)
    stream = Stream(auth, listener)
    gimme = stream.filter(track=["lol"])
    html = "<html><body>It is now %s.</body></html>" % gimme
    return HttpResponse(html)

【讨论】:

    猜你喜欢
    • 2016-08-11
    • 2011-10-30
    • 1970-01-01
    • 2013-01-07
    • 2015-05-15
    • 2021-07-09
    • 2017-11-25
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多