python - Python Geo Scrape 使用 Tweepy
问题描述
我正在使用 Twitter 的 API,并tweepy
希望从 Tweets 中抓取可用的地理位置坐标。我的最终目标是仅将每条推文的坐标存储在表格中。
我的问题是,当定位推文时,我遇到了一个错误,其中提供了比坐标更多的信息:
到目前为止,我的代码如下:
import pandas as pd
import json
import tweepy
import csv
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if status.retweeted:
return
if True:
coords = status.coordinates
geo = status.geo
if geo is not None:
geo = json.dumps(geo)
if coords is not None:
coords = json.dumps(coords)
print(coords, geo)
with open('coordinates_data.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow([coords,geo])
def on_error(self, status_code):
if status_code == 420:
#returning False in on_error disconnects the stream
return False
LOCATIONS = [-124.7771694, 24.520833, -66.947028, 49.384472, # Contiguous US
-164.639405, 58.806859, -144.152365, 71.76871, # Alaska
-160.161542, 18.776344, -154.641396, 22.878623] # Hawaii
auth = tweepy.OAuthHandler('access auths', 'access auths')
auth.set_access_token('token','token')
api = tweepy.API(auth)
user = api.me()
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(locations=LOCATIONS)
我确定这个问题与我缺乏对“json”的理解有关,或者我需要使用数据字典。
我将不胜感激任何帮助!
解决方案
推荐阅读
- flutter - 将列表中的颜色传递给小部件 - Flutter
- excel - 在 Excel VBA 中正确编写公式
- spring-boot - Kubernetes 发现定位器上的 Spring Cloud Gateway 使用 pod 端口而不是服务端口
- arrays - 合并两个单元格 1) if/else 或 2) AND
- javascript - Vue反应性问题:子组件中的插槽
- .net - RSA 和 OAEP:选择还是不选择哈希算法?
- python - 如何更改 tkinter 小部件中的边框颜色?
- r - 全局设置种子 R 3.6.1
- reactjs - 如何仅更新 1 个字段而不覆盖 React Hooks 上的嵌套对象上的其他字段使用 Reducer?
- oracle - Oracle 11 Merge 语句不更新行