为了研究目的,我想做一小部分推文,但是很多工具似乎超出了我的需求。唯一的要求是在本地机器上有包含特定单词的tweet。
至于Python实现,python-twitter
有twitter-stream-sample
工具,但它只转储消息本身,没有元数据。这个包和其他包,比如tweethon
,也缺乏使用流API的文档和示例。
语言并不重要,命令行工具也会受到欢迎。
发布于 2014-08-19 07:04:54
例如,您可以使用请求API。文档(参见链接)对如何将其与流API一起使用有了一些基本解释。您必须设置OAuth,这在请求文档中也有描述。
然后设置请求:
track = 'the' # <-- whatever keyword you want to track
payload = {'language': 'en', 'track': track,
'stringify_friend_ids': 'true'}
url = "https://stream.twitter.com/1.1/statuses/filter.json"
r = requests.get(url=url, auth=oauth, stream=True, params=payload)
for line in r.iter_lines():
# process the lines
结果是json。别忘了关闭溪流:
...
finally:
print("Closing connection...")
r.connection.close()
编辑:这是授权代码:
REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"
AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize?oauth_token="
ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"
def setup_oauth():
"""Authorize your app via identifier."""
# Request token
oauth = OAuth1(TW_API_KEY, client_secret=TW_API_SECRET)
r = requests.post(url=REQUEST_TOKEN_URL, auth=oauth)
credentials = parse_qs(r.content)
resource_owner_key = credentials.get('oauth_token')[0]
resource_owner_secret = credentials.get('oauth_token_secret')[0]
# Authorize
authorize_url = AUTHORIZE_URL + resource_owner_key
print('Please go here and authorize: ' + authorize_url)
verifier = raw_input('Please input the verifier: ')
oauth = OAuth1(TW_API_KEY,
client_secret=TW_API_SECRET,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier)
# Finally, Obtain the Access Token
r = requests.post(url=ACCESS_TOKEN_URL, auth=oauth)
credentials = parse_qs(r.content)
token = credentials.get('oauth_token')[0]
secret = credentials.get('oauth_token_secret')[0]
return token, secret
def get_oauth():
oauth = OAuth1(TW_API_KEY,
client_secret=TW_API_SECRET,
resource_owner_key=TW_ACCESS_TOKEN,
resource_owner_secret=TW_TOKEN_SECRET)
return oauth
https://stackoverflow.com/questions/25377033
复制相似问题