我想为研究目的制作一小部分推文,但许多工具似乎超出了我的需求。唯一的要求是在本地计算机上包含包含特定单词的推文。
对于Python实现,python-twitter
具有twitter-stream-sample
工具,但它只转储消息本身而没有元数据。这个和其他软件包,如tweethon
,也没有关于使用流API的文档和示例。
语言并不重要,命令行工具也非常受欢迎。
答案 0 :(得分:1)
您可以使用Requests 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