用rtweet

时间:2017-11-15 16:11:11

标签: r api twitter rtweet

我试图找到一种方法来使用 rtweet 包从Twitter REST API执行对GET friendships/show的请求,以便分析两个用户之间的关系。但是,我无法使它工作(我确实找到lookup_friendships(),但它没有做我想要的)。是否可以使用 rtweet 执行此操作,如果是,如何执行此操作?

2 个答案:

答案 0 :(得分:1)

此功能已添加到rtweet,因此至少应该合理地访问" friendships / show" API通过lookup_friendships() - 而奖励,它现在已经在CRAN上的版本0.6.0中被接受了!

必需的输入是sourcetarget(可以是屏幕名称或用户ID)。如果只向sourcetarget提供了一个用户,则可以在其他参数中传递任意数量的用户 - 保存Twitter API速率限制。

fds <- lookup_friendships(
  source = "realDonaldTrump",
  target = c("DRUDGE_REPORT", "seanhannity", "HuffPost", "maddow", "cnn")
)
> fds
# A tibble: 60 x 4
   relationship            user       variable           value
          <chr>           <chr>          <chr>           <chr>
 1       source   DRUDGE_REPORT             id        25073877
 2       source   DRUDGE_REPORT         id_str        25073877
 3       source   DRUDGE_REPORT    screen_name realDonaldTrump
 4       source   DRUDGE_REPORT      following            TRUE
 5       source   DRUDGE_REPORT    followed_by           FALSE
 6       source   DRUDGE_REPORT live_following           FALSE
 7       source   DRUDGE_REPORT         can_dm           FALSE
 8       target realDonaldTrump             id        14669951
 9       target realDonaldTrump         id_str        14669951
10       target realDonaldTrump    screen_name   DRUDGE_REPORT
# ... with 50 more rows
> 

还可以在sourcetarget参数中指定多个用户。但是,如果两个参数的长度都大于1,则向量必须具有相等的长度。

## vector of multiple users
usrs <- c(
  "realDonaldTrump", "DRUDGE_REPORT", "seanhannity", "HuffPost", "maddow", "cnn"
)
fds <- lookup_friendships(
  source = usrs, 
  target = usrs
)
> fds
# A tibble: 72 x 4
   relationship            user       variable           value
          <chr>           <chr>          <chr>           <chr>
 1       source realDonaldTrump             id        25073877
 2       source realDonaldTrump         id_str        25073877
 3       source realDonaldTrump    screen_name realDonaldTrump
 4       source realDonaldTrump      following           FALSE
 5       source realDonaldTrump    followed_by           FALSE
 6       source realDonaldTrump live_following           FALSE
 7       source realDonaldTrump         can_dm            TRUE
 8       target realDonaldTrump             id        25073877
 9       target realDonaldTrump         id_str        25073877
10       target realDonaldTrump    screen_name realDonaldTrump
# ... with 62 more rows

还没有很多时间来测试它。如果您遇到问题,请尝试设置parse = FALSE。您可以自己处理返回的数据,但这可能有助于避免内部解析中的错误。

fds <- lookup_friendships(
      source = usrs, 
      target = usrs,
      parse = FALSE
    )
> str(fds, 3)
List of 6
 $ realDonaldTrump:List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ DRUDGE_REPORT  :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ seanhannity    :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ HuffPost       :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ maddow         :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7
 $ cnn            :List of 1
  ..$ relationship:List of 2
  .. ..$ source:List of 16
  .. ..$ target:List of 7

答案 1 :(得分:0)

您可以使用get_friends()get_followers()功能。