【问题标题】:Getting location of tweets in Twitter API and grouping by State在 Twitter API 中获取推文的位置并按状态分组
【发布时间】:2019-03-01 00:03:33
【问题描述】:

我正在尝试在 twitter API 中搜索带有给定文本的推文。我只想在美国完成推文(认为 bio_location 不是地理编码,因为大多数都没有地理编码)。我真的很挣扎如何使用 Tweepy 和 twitter 包来实现这一点。

place = api.geo_search(query="USA", granularity="country")
place_id = place[0].id


for tweet in tweepy.Cursor(api.search,q= "place:%s" % place_id, count=100,
                           text = "SOME TEXT",
                           lang="en",
                           since="2018-2-21").items():
    print (tweet.created_at, tweet.text, tweet.coordinates)
    csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8'), tweet.place])

我希望我的输出是这样的:

推文日期位置

一些推文 2-23-2019 阿拉巴马州

我怎样才能做到这一点?我需要每个州的纬度/经度/半径列表吗?肯定有人这样做过吗?

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    这是我在我的一个项目中用来解决此问题的一些代码。这会尝试在推文元数据中的几个不同位置查找地点数据,如果找不到则返回 None

    states = {
                'AL': 'Alabama',
                'AK': 'Alaska',
                'AZ': 'Arizona',
                'AR': 'Arkansas',
                'CA': 'California',
                'CO': 'Colorado',
                'CT': 'Connecticut',
                'DE': 'Delaware',
                'DC': 'District of Columbia',
                'FL': 'Florida',
                'GA': 'Georgia',
                'HI': 'Hawaii',
                'ID': 'Idaho',
                'IL': 'Illinois',
                'IN': 'Indiana',
                'IA': 'Iowa',
                'KS': 'Kansas',
                'KY': 'Kentucky',
                'LA': 'Louisiana',
                'ME': 'Maine',
                'MD': 'Maryland',
                'MA': 'Massachusetts',
                'MI': 'Michigan',
                'MN': 'Minnesota',
                'MS': 'Mississippi',
                'MO': 'Missouri',
                'MT': 'Montana',
                'NE': 'Nebraska',
                'NV': 'Nevada',
                'NH': 'New Hampshire',
                'NJ': 'New Jersey',
                'NM': 'New Mexico',
                'NY': 'New York',
                'NC': 'North Carolina',
                'ND': 'North Dakota',
                'OH': 'Ohio',
                'OK': 'Oklahoma',
                'OR': 'Oregon',
                'PA': 'Pennsylvania',
                'RI': 'Rhode Island',
                'SC': 'South Carolina',
                'SD': 'South Dakota',
                'TN': 'Tennessee',
                'TX': 'Texas',
                'UT': 'Utah',
                'VT': 'Vermont',
                'VA': 'Virginia',
                'WA': 'Washington',
                'WV': 'West Virginia',
                'WI': 'Wisconsin',
                'WY': 'Wyoming'
             }
    
    def extract_place(status):
        if type(status) is tweepy.models.Status:
            status = status.__dict__
        #Try to get the place from the place data inside the status dict
        if status['place'] is not None:
            place = status['place']
            if place['country'] != 'United States':
                return place['country']
            elif place['place_type'] == 'admin':
                return place['name']
            elif place['place_type'] == 'city':
                return states.get(place['full_name'].split(', ')[-1])
        #If the status dict has no place info, get the place from the user data
        else:
            place = status['user']['location']
            try:
                place = place.split(', ')[-1].upper()
            except AttributeError:
                return None
            if place in states:
                return states[place]
            else:
                return place
    

    【讨论】:

      猜你喜欢
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 2015-06-21
      相关资源
      最近更新 更多