使用 rtweet 获取 friendships/show

GET friendships/show with rtweet

我试图找到一种方法来使用 rtweet 包从 Twitter REST API 执行对 GET friendships/show 的请求,以便分析两个用户之间的关系。但是,我无法让它工作(我确实找到了 lookup_friendships(),但它并没有按照我的意愿进行)。 rtweet 可以做到这一点吗?如果可以,怎么做?

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

此功能已添加到 rtweet,因此至少应该可以通过 lookup_friendships() 合理地访问 "friendships/show" API – 而且,奖金,它刚刚在 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