是否可以从R会话更新我的Facebook状态?
编辑1:阅读到目前为止的回复,我想指出,我只是感兴趣,如果已经存在提供此功能的包,类似于可爱的twitteR包为twitter做的.此外,为了"有趣",某些东西不一定是"有用的",这是我喜欢学习的方式.
编辑2:对于任何被我冒犯的人抱歉,因为我没有更具体地提出我的问题.我已经非正式使用了R 2个月,并被告知SO是一个提问的好地方(是的,我已经阅读了介绍指南).
Ton*_*yal 10
注意:以下只能成功登录到facebook.我不知道为什么最后的状态更新不起作用,但也许它仍然有一些价值.它基于3月份在Baratttalo的一篇博客文章,我认为这将在周五下午消磨时间.
我不打算回答这个问题,但看看其他一些回复并看到你在mathoverflow上帮助我,我想我会试一试.
你需要安装来自http://www.omegahat.org/的RCurl和XML软件包(这是一个非常酷的网站,我认为它只是为了好玩).
无论如何复制并粘贴这个:
library(RCurl)
library(XML)
log.into.facebook <- function(curl, id) {
curlSetOpt( .opts = list(postfields = paste('email=', URLencode(id$login.email), '&pass=', URLencode(id$login.password), '&login=', URLencode('"Login"'), sep=''),
post = TRUE,
header = FALSE,
followlocation = TRUE,
ssl.verifypeer = FALSE,
cookiejar = 'my_cookies.txt',
cookiefile = 'my_cookies.txt',
useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'), curl = curl)
u <- "https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php"
doc <- getURL(u, curl = curl)
return(doc)
}
get.update.stutus.form.id <- function(curl, doc) {
curlSetOpt( .opts = list(post = FALSE), curl = curl)
doc <- getURL("http://m.facebook.com/home.php" , curl = curl)
html <- htmlTreeParse(doc, useInternal = TRUE)
# this gets the post_form_id value
form.id.node <- getNodeSet(html, '//input[@name="post_form_id"]')
form.id <- sapply(form.id.node, function(x) x <- xmlAttrs(x)[[3]])
# we'll also need the exact name of the form processor page
form.num.node <- getNodeSet(html, '//form[@method="post"]')
form.num <- sapply(form.num.node, function(x) x <- xmlAttrs(x)[[1]])
form.num <- strsplit(form.num, "/")[[1]][3]
return(list(form.id = form.id, form.num = form.num))
}
# This function doesn't work. I would love to know why though as it 'looks' right to me
update.status <- function(doc, curl, id) {
form <- get.update.stutus.form.id (curl, doc)
curlSetOpt( .opts = list(post = TRUE,
postfields = paste('post_form_id=', form$form.id, '&status=', URLencode(id$status), '&update=', URLencode('"Update status"'), sep = '')),
curl = curl)
u <- paste("http://m.facebook.com", form$form.num, sep = "/")
doc <- getURL(u, curl = curl)
return(doc)
}
Run Code Online (Sandbox Code Playgroud)
以下是您使用上述功能的方法(将ID值更改为您的日志详细信息)
id <- list()
id$status <- "Hello world!"
id$login.email <- "YOUR LOGIN EMAIL"
id$login.password <- "YOUR LOGIN PASSWORD"
# log into facebook, seems to work fine
curl <- getCurlHandle()
doc <- log.into.facebook(curl, id)
# this is the bit that doesn't work, no idea why though.
update.status(doc, curl, id)
Run Code Online (Sandbox Code Playgroud)
希望有所帮助,也许它会给你一个想法.另外,我认为你问的问题很好,下次可能会更具体一点,所以也许你会避免一些你在这里得到的评论:-)
Tony Breyal
PS我觉得这个地方有一个api,但如果你感兴趣的是更新状态,我非常喜欢使用twitteR包并将更新链接到facebook.
当然,研究 API 并创建一个包。
如果您的问题确实是“有人已经为我完成了这项工作吗?” 那么答案可能是否定的。
针对该评论,经典的“这就是 R。没有如果。只有如何。 ”仍然适用。引用fortunes包中的内容:
> library(fortunes)
> fortune("Yoda")
Evelyn Hall: I would like to know how (if) I can extract some of the
information from the summary of my nlme.
Simon Blomberg: This is R. There is no if. Only how.
-- Evelyn Hall and Simon 'Yoda' Blomberg
R-help (April 2005)
>
Run Code Online (Sandbox Code Playgroud)
简而言之,下载twitteR包,看看它如何使用RCurl包访问 Web API,并对 Facebook 的 API 执行同样的操作。或者花钱找人帮你做。
归档时间: |
|
查看次数: |
2141 次 |
最近记录: |