2013-02-04 25 views
3

有沒有什麼辦法可以在特定的時間範圍內(比如12月和1月之間)使用twitteR獲取推文,而不是簡單地使用過去的N個推文(如推文< - UserTimeline(用戶,N = 1000)?推特在特定的時間跨度(TwitteR)

或者是不能使用Twitter的圖書館嗎?(這意味着你必須使用類似Excel來子集了大量的日期推特)的。

+0

看來, Twitter API在查詢用戶時間線時不支持時間跨度:https://dev.twitter.com/docs/api/1/get/statuses/user_timeline – orizon

回答

3

在包你重新使用時,searchTwitter函數採用參數sinceuntil,在documentation中定義如下:

如果不是NULL,則會將自推薦日期以來的推文限制爲推文。 日期將被格式化爲YYYY-MM-DD

直到如果不爲NULL,限制鳴叫那些直到給定日期。 日期將被格式化爲YYYY-MM-DD

這就是你所追求的嗎?或者,如果您想堅持使用userTimeline函數,則可以通過對status對象的created字段進行操作(因此不需要使用Excel)來對所需的日期範圍進行子集分類。

編輯下面是你可能在created場,如果你正在使用userTimeline子集:

library(twitteR) 
# get last 100 tweets from the NSF 
tweets <- userTimeline('NSF', 100) 
# inspect structure of first item in the status object (ie. list of results) 
str(tweets[1]) 
List of 1 
$ :Reference class 'status' [package "twitteR"] with 10 fields 
    ..$ text  : chr "From the field: Avoiding a Cartography Catastrophe: Study recommends new tools to improve global mapping of inf... http://t.co"| __truncated__ 
    ..$ favorited : logi FALSE 
    ..$ replyToSN : chr(0) 
    ..$ created  : POSIXct[1:1], format: "2013-02-05 01:43:45" 
    ..$ truncated : logi FALSE 
    ..$ replyToSID : chr(0) 
    ..$ id   : chr "298607815617036288" 
    ..$ replyToUID : chr(0) 
    ..$ statusSource: chr "<a href=\"http://twitterfeed.com\" rel=\"nofollow\">twitterfeed</a>" 
    ..$ screenName : chr "NSF" 
    ..and 34 methods, of which 23 are possibly relevant: 
    .. getCreated, getFavorited, getId, getReplyToSID, getReplyToSN, 
    .. getReplyToUID, getScreenName, getStatusSource, getText, 
    .. getTruncated, initialize, setCreated, setFavorited, setId, 
    .. setReplyToSID, setReplyToSN, setReplyToUID, setScreenName, 
     .. setStatusSource, setText, setTruncated, toDataFrame, usingMethods 


# convert status object to data frame for easier manipulation 
tweetsdf <- twListToDF(tweets) 


# subset by `created` field, eg get all tweets between 2 Feb and 5 Feb 
    subset(tweetsdf, created >= as.POSIXct('2013-02-02 00:00:00') & created <= as.POSIXct('2013-02-05 00:00:00')) 

及這裏的該子集中的操作產生的數據框:

text 
1 From the field: Avoiding a Cartography Catastrophe: Study recommends new tools to improve global mapping of inf... http://t.co/F6IJ05Sb 
2     Video: Research Vessel Sikuliaq launched... and now being prepared for her first Arctic run in 2014, http://t.co/D7GlRnlm 
3                      Who's watching the power grid? http://t.co/oYsgBl63 
4 Ice Melt &amp; the Ice Age... research story on #AAAS #Science Update Daily, featured show @Science360 Radio, http://t.co/XRXSdYL1 #Arctic 
5                        Taking LIGO to the people http://t.co/R2KHNQTB 
6       Pubs: NSF Current - January-February 2013: Available Formats: JSP: http://t.co/2NhEEj6Q... http://t.co/ZSVABpXm 
7 Upcoming Due Dates: Interdisciplinary Research in Hazards and Disasters (Hazards SEES): Full Proposal Deadline D... http://t.co/IG3naAFs 
8              When children learn to walk, their language improves dramatically http://t.co/FGYXSKu2 
    favorited replyToSN    created truncated replyToSID 
1  FALSE  NA 2013-02-05 01:43:45  FALSE   NA 
2  FALSE  NA 2013-02-04 19:30:40  FALSE   NA 
3  FALSE  NA 2013-02-04 18:01:33  FALSE   NA 
4  FALSE  NA 2013-02-04 13:55:46  FALSE   NA 
5  FALSE  NA 2013-02-04 13:01:51  FALSE   NA 
6  FALSE  NA 2013-02-02 17:19:30  FALSE   NA 
7  FALSE  NA 2013-02-02 14:25:15  FALSE   NA 
8  FALSE  NA 2013-02-02 14:02:11  FALSE   NA 
        id replyToUID 
1 298607815617036288   NA 
2 298513923307630592   NA 
3 298491499958644736   NA 
4 298429645580288000   NA 
5 298416076012785666   NA 
6 297756138433290240   NA 
7 297712287521841156   NA 
8 297706485608218624   NA 
                statusSource 
1 <a href="http://twitterfeed.com" rel="nofollow">twitterfeed</a> 
2 <a href="http://www.hootsuite.com" rel="nofollow">HootSuite</a> 
3 <a href="http://www.hootsuite.com" rel="nofollow">HootSuite</a> 
4 <a href="http://www.hootsuite.com" rel="nofollow">HootSuite</a> 
5 <a href="http://www.hootsuite.com" rel="nofollow">HootSuite</a> 
6 <a href="http://twitterfeed.com" rel="nofollow">twitterfeed</a> 
7 <a href="http://twitterfeed.com" rel="nofollow">twitterfeed</a> 
8 <a href="http://www.hootsuite.com" rel="nofollow">HootSuite</a> 
    screenName 
1  NSF 
2  NSF 
3  NSF 
4  NSF 
5  NSF 
6  NSF 
7  NSF 
8  NSF 
+0

我認爲他使用'''userTimeline'''好玩但如果它可以被限制在單個用戶的推文上,''''searchTwitter'''可能會支持它。 – orizon

+0

是的,我看到,似乎要麼在特定日期範圍內爲用戶獲取特定用戶的推文,而是要將'user'(如果使用'searchTwitter')或'created'(如果使用'userTimeline')子集。看起來好像沒有爲此目的的單線程...... – Ben

+0

如何訪問創建的狀態對象字段?如果我的對象是推文< - userTimeline(用戶,100),我會有像子集(微博,微博@創建== 2012-11-12,微博@創建== 2012-12-12)? – Emir