尝试使用tweepy从twitter标签提要中捕获数据

时间:2015-05-31 23:08:57

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

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

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

1 个答案:

答案 0 :(得分:0)

所以,我设法在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)