【发布时间】:2018-03-07 22:29:59
【问题描述】:
我一直在使用 Tweepy 通过流 API 收集某个区域的推文,我只收集推文的纬度/经度,但我想添加更多内容,但我不确定是什么具体是。我正在使用这段代码来获取纬度/经度值:
import json, tweepy
from html.parser import HTMLParser
consumer_key = ""
consumer_secret = ""
access_token = ""
access_secret = ""
count = 0
class StdOutListener(tweepy.StreamListener):
def on_data(self, data):
global count
decoded = json.loads(HTMLParser().unescape(data))
if decoded.get('coordinates',None) is not None:
coordinates = decoded.get('coordinates','').get('coordinates','')
name = decoded.get('name','')
with open("C:\\Users\\gchre\\Desktop\\Tweets.txt", "a") as text_file:
print(decoded['coordinates'], file=text_file)
print(decoded['coordinates'])
count += 1
return True
def on_error(self, status):
print(status)
l = StdOutListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
stream = tweepy.Stream(auth, l)
while count < 1000000:
stream.filter(locations=[-88.853859,41.220047,-86.953073,42.758134])
我希望这也能将特定用户名 (@handle) 和推文创建时间打印到文本文件中。我不确定是否应该在 if decoded.get('coordinates',None) is not None: 循环中执行此操作。
【问题讨论】:
标签: python python-3.x twitter tweepy