2017-06-16 56 views
-1

此代碼打開一個twitter偵聽器,搜索條件位於變量upgrades_str中。有些搜索工作,有些搜索沒有。我將AMZN添加到升級列表中,只是爲了確保有一個經常使用的術語,因爲這是使用開放的Twitter流,而不是搜索現有的推文。Twitter API,使用美元符號搜索

下面,我想我們只需要在Windows 10

審查號碼2和4

我使用Python 3.5.2 ::蟒蛇4.0.0(64位)可變搜索

  1. 與搜索:upgrades_str: 'AMZN', 'SWK', 'AIQUY', 'SFUN', '門'] =返回微博,如「我已經厭倦了人們

  2. 搜索with:upgrades_str:['$ AMZN','$ SWK','$ AIQUY','$ SFUN','$ DOOR'] =返回tweets,如'芝加哥到南佛羅里達。嘻哈生活'。 此搜索是我希望工作的人。

顯式的搜索

  • 通過與顯式字符串替換變量 'upgrades_str' 在搜索:[ 'AMZN', 'SWK',' AIQUY','SFUN','DOOR'] =返回'在進入兩次後,我終於想出瞭如何在瑞典鎖門的方法。這個至少有搜索詞'門'。

  • 用顯式字符串替換變量'upgrades_str'進行搜索:['$ AMZN','$ SWK','$ AIQUY','$ SFUN','$ DOOR'] =返回'$ AMZN $ WFM $ KR $ REG $ KIM:亞馬遜的Whole Foods購買將購物中心視爲真正的風險。 所以顯式調用起作用,但不是相同的變量。

  • 明確搜索[「$ AMZN」] =返回一個良好的鳴叫:「方設立下週真的很不錯!新增$ googl jun23 970c平均4.36。 $ FB $ AMZN'。

  • 顯式搜索['cool']會返回'我不相信我有這麼酷的枕頭!

    import tweepy 
    import dataset 
    from textblob import TextBlob 
    from sqlalchemy.exc import ProgrammingError 
    import json 
    
    db = dataset.connect('sqlite:///tweets.db') 
    
    class StreamListener(tweepy.StreamListener): 
    
    def on_status(self, status): 
        if status.retweeted: 
         return 
    
        description = status.user.description 
        loc = status.user.location 
        text = status.text 
        coords = status.coordinates 
        geo = status.geo 
        name = status.user.screen_name 
        user_created = status.user.created_at 
        followers = status.user.followers_count 
        id_str = status.id_str 
        created = status.created_at 
        retweets = status.retweet_count 
        bg_color = status.user.profile_background_color 
        blob = TextBlob(text) 
        sent = blob.sentiment 
    
        if geo is not None: 
         geo = json.dumps(geo) 
    
        if coords is not None: 
         coords = json.dumps(coords) 
    
        table = db['tweets'] 
        try: 
         table.insert(dict(
          user_description=description, 
          user_location=loc, 
          coordinates=coords, 
          text=text, 
          geo=geo, 
          user_name=name, 
          user_created=user_created, 
          user_followers=followers, 
          id_str=id_str, 
          created=created, 
          retweet_count=retweets, 
          user_bg_color=bg_color, 
          polarity=sent.polarity, 
          subjectivity=sent.subjectivity, 
         )) 
        except ProgrammingError as err: 
         print(err) 
    
    def on_error(self, status_code): 
        if status_code == 420: 
         return False 
    
    access_token = 'token' 
    access_token_secret = 'tokensecret' 
    consumer_key = 'consumerkey' 
    consumer_secret = 'consumersecret' 
    
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_token, access_token_secret) 
    api = tweepy.API(auth) 
    
    stream_listener = StreamListener() 
    stream = tweepy.Stream(auth=api.auth, listener=stream_listener) 
    stream.filter(track=upgrades_str, languages=['en']) 
    
  • 回答

    0

    這裏的答案,萬一有人在將來出現問題:「請注意,標點符號不被認爲是一個#hashtag或@mention的一部分,所以含標點符號的軌道內不會匹配#hashtags或@mentions。「來自:https://dev.twitter.com/streaming/overview/request-parameters#track

    對於多個術語,從列表轉換而來的字符串需要更改爲['term1,term2']。只刪除撇號和空格:

    upgrades_str = re.sub('[\' \[\]]', '', upgrades_str) 
    upgrades_str = '[\''+format(upgrades_str)+'\']'