分享一个我的公众号,最近突然想玩公众号,之前做过一段时间前端开发,考虑到现在应用程序越来越多,未来社会一定是一个充满“只有你想不到,没有你做不到”的App的世界!而微信小程序又给我们这群喜爱开发的童鞋们提供了友好的入门机会,因此,不如就在当下,开始一步一步的学习并开发自己的小程序吧。这是我的公众号:目的是和大家讨论小程序开发,同时也带着开发小白入门设计开发微信小程序,来吧,加入!

正文:

 
我需要爬取的用户ID存放在一个.csv文件下,然后从官网注册到一个APP,并获得你的key和secret,写入下边的代码,就可以爬取tweets了。
每个ID会输出相应的tweet并且s会放在一个.csv文件里,而这个.csv文件就在你运行这段代码的文件夹下。
#!/usr/bin/env python
# encoding: utf-8
import tweepy
import csv
 
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
 
 
def get_all_tweets(user_id):
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)
 
    # 初始化一个数字来存储所有的tweets
    alltweets = []
 
    new_tweets = api.user_timeline(user_id=user_id, count=200)
 
    # save most recent tweets
    alltweets.extend(new_tweets)
 
    # save the id of the oldest tweet less one
    oldest = alltweets[-1].id - 1
 
    # keep grabbing tweets until there are no tweets left to grab
    while len(new_tweets) > 0:
        print "getting tweets before %s" % (oldest)
 
        # all subsiquent requests use the max_id param to prevent duplicates
        new_tweets = api.user_timeline(user_id=user_id, count=200, max_id=oldest)
 
        # save most recent tweets
        alltweets.extend(new_tweets)
 
        # update the id of the oldest tweet less one
        oldest = alltweets[-1].id - 1
 
        print "...%s tweets downloaded so far" % (len(alltweets))
 
    # transform the tweepy tweets into a 2D array that will populate the csv
    outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
 
    # write the csv
    with open('%s_tweets.csv' % user_id, 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(["tweet_id", "created_at", "text"])
        writer.writerows(outtweets)
    pass
 
 
if __name__ == '__main__':
    with open(这里写你的文件的位置,例如:'e:/file/userID.csv', 'rb') as f:
        ID = csv.reader(f)
        for row in ID:
# 这里运用了错误查询机制,遇到用户ID出现问题时,可以跳过
            try:
                get_all_tweets(row[0])
            except tweepy.TweepError, e:
                print 'Failed to run the command on that user, Skipping...'
            except IndexError, e:
                print 'List index out of range, Skipping...'
                continue
           
 
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐