본문 바로가기
파이썬

파이썬 트 위피 모듈에 위치 필터를 추가하는 방법

by º기록 2020. 12. 16.
반응형

Python Shell에서 트위터 firehose의 표준 1 %를 볼 수있게 해주는 다음 코드를 찾았습니다.

import sys
import tweepy

consumer_key=""
consumer_secret=""
access_key = ""
access_secret = "" 


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)


class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print status.text

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['manchester united'])

특정 위치의 트윗 만 구문 분석하는 필터를 추가하려면 어떻게해야합니까? 나는 사람들이 다른 트위터 관련 Python 코드에 GPS를 추가하는 것을 보았지만 Tweepy 모듈 내에서 sapi와 관련된 것을 찾을 수 없습니다.

어떤 아이디어?

감사

 

해결 방법

 

스트리밍 API는 위치 및 키워드별로 동시에 필터링 할 수 없습니다.

경계 상자는 다른 필터 매개 변수에 대한 필터로 작동하지 않습니다. 예를 들면 track=twitter&locations=-122.75,36.8,-121.75,37.8 would match any tweets containing Twitter (지역이 아닌 트윗 포함)라는 용어 또는 샌프란시스코 지역에서 온 용어입니다.


할 수있는 일은 스트리밍 API에 키워드 또는 찾은 트윗을 요청한 다음 각 트윗을 조사하여 앱에서 결과 스트림을 필터링하는 것입니다.

다음과 같이 코드를 수정하면 영국에서 트윗을 캡처 한 다음 해당 트윗이 필터링되어 "manchester united"가 포함 된 트윗 만 표시됩니다.

import sys
import tweepy

consumer_key=""
consumer_secret=""
access_key=""
access_secret=""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)


class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        if 'manchester united' in status.text.lower():
            print status.text

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

sapi = tweepy.streaming.Stream(auth, CustomStreamListener())    
sapi.filter(locations=[-6.38,49.87,1.77,55.81])

 

참조 페이지 https://stackoverflow.com/questions/22889122

 

 

반응형

댓글