เป้าหมายคือการใช้ Python เพื่อค้นหาทวีตที่มีคำที่ระบุ จับพิกัดละติจูด/ลองจิจูด และเขียนลงใน .csv
อัปเดต: ในขณะนี้ มันส่งคืน "ไม่มีพิกัด" ไม่ว่าฉันจะใช้คำหลักใดก็ตาม
#All of the consumer keys, tokens, and imports are left out of this sample code.
file = open("C:\Scripts\Output.csv", "w")
file.write("X,Y\n")
data_list = []
count = 0
class listener(StreamListener):
def on_data(self, data):
global count
#How many tweets you want to find, could change to time based
if count <= 2000:
json_data = json.loads(data)
coords = json_data["coordinates"]
if coords is None:
print("no coordinates")
else:
print(coords["coordinates"])
lon = coords["coordinates"][0]
lat = coords["coordinates"][1]
data_list.append(json_data)
file.write(str(lon) + ",")
file.write(str(lat) + "\n")
count += 1
return True
else:
file.close()
return False
def on_error(self, status):
print(status)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())
#What you want to search for here
twitterStream.filter(track=["horse"])
ผลลัพธ์ที่คาดหวังจะเป็นไฟล์ CSV ที่มีพิกัดละติจูดและลองจิจูดของทวีตที่มีคำว่า "ม้า" อัปเดต: มันส่งคืน "ไม่มีพิกัด" สำหรับคำหลักใดก็ตามที่ฉันลองใช้