在Twitter API中获取推文的位置并按州分组

问题描述 投票:0回答:1

我正在尝试使用给定文本在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个回答
0
投票

以下是我在其中一个项目中解决此问题的一些代码。这会尝试在tweet元数据中的几个不同位置找到位置数据,如果不能,则返回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
© www.soinside.com 2019 - 2024. All rights reserved.