2015-10-01 17 views
0

以下是我從twitter流中讀取數據的代碼。當我嘗試通過終端運行它時,不會返回任何數據,也不會返回任何錯誤。下面通過tweepy連接到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']) 

回答

0

有幾件事情不正確的代碼,當我殺掉該進程將返回以下回溯。

  1. on_status不返回JSON對象返回ResultSet。這些信息可通過user_handle=status.user.screen_name
  2. 訪問。on_status函數中不需要for循環。當獲得每條推文時,它自行循環。

修復這些錯誤,看看是否改變了任何東西。

相關問題