从推特列表中获取推特屏幕名称

时间:2015-05-05 21:32:17

标签: r twitter

我希望使用R从特定的推文列表中获取用户名和全名的列表。  我无法在任何包中看到函数,但此代码可以正常工作

library(XML)
library(httr)


url.name <- "https://twitter.com/TwitterUK/lists/premier-league-players/members"
url.get=GET(url.name)
url.content=content(url.get, as="text")
pagehtml <- htmlParse(url.content)

screenNames <-xpathSApply(pagehtml, '//*/span[@class="username js-action-profile-name"]',xmlValue)
realName <- xpathSApply(pagehtml, '//*/strong[@class="fullname js-action-profile-name"]',xmlValue)

但是,它只提供前20个值(?屏幕上显示的内容),而列表更长

如果有一个rvest解决方案,这也是受欢迎的

欢呼声

1 个答案:

答案 0 :(得分:0)

Molx的解决方案似乎不再适用。问题似乎在于

api.url <- paste0("https://api.twitter.com/1.1/lists/members.json?slug=",
           twlist, "&owner_screen_name=", twowner, "&count=5000")

对于我尝试过的任何twlist或twowner,此网址似乎无效。 编辑:问题来自我认为的身份验证

{"errors":[{"code":215,"message":"Bad Authentication data."}]}

我想我已经通过此验证

## Twitter authentication, 
consumer_key = "xxxxx"
consumer_secret = "xxx"
access_token = "xxxxx"
access_secret = "xxx"
setup_twitter_oauth(consumer_key, consumer_secret, access_token,
access_secret)

问题出在哪里?

编辑:当我输入get_oauth_sig()时,我会得到以下结果

> twitteR:::get_oauth_sig()
<Token>
NULL
<oauth_app> twitter
  key:    XXXXXXX
  secret: <hidden>
<credentials> oauth_token, oauth_token_secret
---

这是正常的吗?

Molx的解决方案似乎不再适用。问题似乎在于

api.url <- paste0("https://api.twitter.com/1.1/lists/members.json?slug=",
           twlist, "&owner_screen_name=", twowner, "&count=5000")

对于我尝试过的任何twlist或twowner,此网址似乎无效。 编辑:问题来自我认为的身份验证

{"errors":[{"code":215,"message":"Bad Authentication data."}]}

我想我已经通过此验证

## Twitter authentication, 
consumer_key = "xxxxx"
consumer_secret = "xxx"
access_token = "xxxxx"
access_secret = "xxx"
setup_twitter_oauth(consumer_key, consumer_secret, access_token,
access_secret)

问题出在哪里?

编辑:当我输入get_oauth_sig()时,我会得到以下结果

> twitteR:::get_oauth_sig()
<Token>
NULL
<oauth_app> twitter
  key:    XXXXXXX
  secret: <hidden>
<credentials> oauth_token, oauth_token_secret
---

这是正常的吗?

编辑:我通过GET替换POST来解决问题

library(rjson)
library(twitteR)
consumer_key = "xxxxx"
consumer_secret = "xxx"
access_token = "xxxxx"
access_secret = "xxx"
setup_twitter_oauth(consumer_key, consumer_secret, access_token,
access_secret)
https://twitter.com/ivalerio/lists/justice?lang=fr
twlist <- "d-put-s-2017-2022"
twowner <- "ivalerio"
api.url <- paste0("https://api.twitter.com/1.1/lists/members.json?slug=",
           twlist, "&owner_screen_name=", twowner, "&count=5000")
response <- GET(api.url, config(token=twitteR:::get_oauth_sig()))
#Count = 5000 is the number of names per result page,
#        which for this case simplifies things to one page.
# This returns a JSON response which we can read using fromJSON:
response.list <- fromJSON(content(response, as = "text", encoding = "UTF-8"))
# Now, we have a list where each element is the Twitter data of one Twitter-list member. To extract their names and user_names:
users.names <- sapply(response.list$users, function(i) i$name)
users.screennames <- sapply(response.list$users, function(i) i$screen_name)
# Which are:
head(users.names)