通过tweepy连接到twitter流时没有返回错误或数据

时间:2015-10-01 20:45:49

标签: python twitter tweepy twitter-streaming-api

以下是我从twitter流中读取数据的代码。当我尝试通过终端运行它时,既不返回数据,也不返回任何错误。当我终止进程时返回以下回溯:

File "Soundcloud.py", line 59, in <module>
    twitter_stream.filter(track=['soundcloud.com'])
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 430, in filter
    self._start(async)
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 346, in _start
    self._run()
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 255, in _run
    self._read_loop(resp)
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 298, in _read_loop
    line = buf.read_line().strip()
  File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 171, in read_line
    self._buffer += self._stream.read(self._chunk_size)
  File "/Library/Python/2.7/site-packages/requests/packages/urllib3/response.py", line 243, in read
    data = self._fp.read(amt)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 588, in read
    return self._read_chunked(amt)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 630, in _read_chunked
    line = self.fp.readline(_MAXLINE + 1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 480, in readline
    data = self._sock.recv(self._rbufsize)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 734, in recv
    return self.read(buflen)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 621, in read
    v = self._sslobj.read(len or 1024)

我的代码如下:

    import tweepy
    from tweepy import Stream
    from tweepy.streaming import StreamListener
    from tweepy import OAuthHandler
    import json
    import time
    import pymysql
    import sys
    import extraction
    import requests
    import codecs
    import urllib2
    import urllib 
    from urllib import urlopen
    from BeautifulSoup import BeautifulSoup

    #twitter Authentication-keys not entered
    consumer_key = ''
    consumer_secret = '' 
    access_token = ''
    access_secret = ''

    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)


    global conn
    conn=pymysql.connect(db='twitter_users', user='root' , host='127.0.0.1' , port=3307)
    global cursor
    cursor=conn.cursor()
    class MyListener(StreamListener):

     def on_status(self, status):
        tweet_json=json.loads(status)
        print(tweet_json)
        for i in tweet_json:
            user_handle=i['user']['screen_name']
            user_followers=i['user']['followers_count']
            user_statuses=i['user']['statuses_count']
            user_location=i['user']['location']
            user_geo=i['geo']
            tweet_place=i['place']
            tweet_device=i['source']
            tweet_id=i['id_str']
            url=i['entities']['urls'][0]['expanded_url']
        print(url)
        return True

     def on_error(self, status):
        print(status)
        return True

     def on_timeout(self):
        print("Received timeout. Sleeping for 20 secs")
        time.sleep(20)
        return True

    twitter_stream = Stream(auth, MyListener())
    twitter_stream.filter(track=['soundcloud.com'])

1 个答案:

答案 0 :(得分:0)

代码中有一些不正确的东西。

  1. on_status未返回返回JSON的{​​{1}}对象。使用ResultSet
  2. 访问这些信息
  3. 您在user_handle=status.user.screen_name函数中不需要for循环。当获得每条推文时,它会自行循环。
  4. 修正这些错误,看看是否有任何改变。