之前一直想用爬虫登陆并抓取twitter数据,试过scrapy,requests等包,都没成功,可能是我还不太熟悉的原因,不过
今天发现了一个新包tweepy,专门用于在python中处理twitter API。先尝试一下教程的第一个例子,经过了自己的一点修改
代码如下:
<pre name="code" class="python">__author__ = 'xyz'
import re
import tweepy
auth = tweepy.OAuthHandler("xxxxx",
"xxxxx")
auth.set_access_token("xxxxx",
"xxxxx")
api = tweepy.API(auth)
highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
public_tweets = api.home_timeline()
num = 0
for tweet in public_tweets:
print num
num += 1
text_noem = highpoints.sub('--emoji--', tweet.text)
text_noem = text_noem.encode('utf8')
print(text_noem)
代码解释:
第3-4行:导入twe