迭代推文并使用tweepy和shapefile将它们保存在shapefile中

时间:2014-12-08 15:26:14

标签: if-statement twitter tweepy shapefile points

我正在搜索推文,并希望将它们保存在shapefile中。通过推文进行迭代进展顺利,当我使用print语句时,我可以得到我想要的。我试图把这些推文放在一个点shapefile中。由于某种原因,它不接受if语句的迭代。那么如何迭代我的推文并在我的点shapefile中逐个保存它们,只附加了tweet.text和tweet.id?

通过查看以下链接,我获得了灵感:https://code.google.com/p/pyshp/

import tweepy
import shapefile

consumer_key = "..."
consumer_secret = "..."
access_token = "..."
access_token_secret = "..."

auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

tweetsaspoints = shapefile.Writer(shapefile.POINT)

page = 1
while True:
    statuses = api.search(q="*",count=1000, geocode="52.015106,5.394287,150km")
    if statuses:
        for status in statuses:
            print status.geo
            tweetsaspoints._shapes.extend([status.geo['coordinates']])
            tweetsaspoints.records.extend([("TEXT","Test")])
    else:
        # All done
        break
    page += 1  # next page

tweetsaspoints.save('shapefiles/test/point')

我不明白页面部分。我似乎一遍又一遍地浏览相同的推文。此外,我没有成功地将我的坐标和数据写入点shapefile。

1 个答案:

答案 0 :(得分:1)

根据文档,尝试使用:

for status in tweepy.Cursor(api.user_timeline).items():
    # process status here
    process_status(status)

替代:

page = 1
while True:
    statuses = api.user_timeline(page=page)
    if statuses:
        for status in statuses:
            # process status here
            process_status(status)
    else:
        # All done
        break
    page += 1  # next page

参考:http://docs.tweepy.org/en/latest/cursor_tutorial.html