【问题标题】:Input of 5 character(digit) ID in reddit URL在 reddit URL 中输入 5 个字符(数字)ID
【发布时间】:2012-07-16 23:20:57
【问题描述】:

在reddit URL中,有“5个字符数字”的thing_id部分(例如,“http://redd.it/wplf7”中的“wplf7”)是由base36生成的。

wplf7 是从数字 54941875 生成的——这是我目前发现的……我想知道 54941875 是如何生成的。

我正在尝试使用 R 抓取 reddit 特定部分的评论(比如说http://www.reddit.com/r/leagueoflegends/),但我被这 5 个字符的数字困住了。

谁能用简单的方式解释一下?不幸的是,Python 不是我的领域,Reddit 网站上列出的 2000 行 Python 代码对我没有多大帮助。

谢谢,

【问题讨论】:

  • 您是在问如何从 54941875 到 wplf7?或者 Reddit 最初是如何生成 54941875 的?
  • @Maruis,我首先问的是 Reddit 是如何生成 54941875 的。
  • @ttmaccer,感谢您的注意。我不知道它相当神秘。我打算将 URL 输入到我的 R 脚本中,并在我解析 HTMLtrees 后只获取注释部分..

标签: r reddit


【解决方案1】:

首先设置一个独特的用户代理,因为 reddit 喜欢这样

options(HTTPUserAgent="My name is BOB")

我假设您想在 http://www.reddit.com/r/leagueoflegends/ 获取内容。您需要将.json 附加到网址:

library(RJSONIO)
library(RCurl)
# library(XML)

jdata<-getURL('http://www.reddit.com/r/leagueoflegends/.json')
jdata<-fromJSON(jdata)
# xdata<-getURL('http://www.reddit.com/r/leagueoflegends/.xml')
# xdata<-xmlParse(xdata)

显然内容非常丰富,例如域名、永久链接、作者、帖子标题:

Domains<-sapply(jdata[[2]]$children,function(x){x$data$domain})
permalinks<-sapply(jdata[[2]]$children,function(x){x$data$permalink})
authors<-sapply(jdata[[2]]$children,function(x){x$data$author})
titles<-sapply(jdata[[2]]$children,function(x){x$data$title})
ids<-sapply(jdata[[2]]$children,function(x){x$data$id})
created<-as.POSIXct(sapply(jdata[[2]]$children,function(x){x$data$created}),origin="1970/01/01")


> head(titles)
[1] "Pendragon 3-day-banning someone for randoming in ranked, or saying hes going to. Mixed feelings..."
[2] "Dig Kicks L0cust."                                                                                 
[3] "Summoners, y u no communicate??"                                                                   
[4] "Without Even Trying"                                                                               
[5] "Cross Country Tryndamere (Chaox Stream)"                                                           
[6] "Top 5 Flops - Episode 4 ft Dyrus, Phantoml0rd, and HatPerson vs Baron Nashor"                      
> 

要调查这些 id 是如何生成的,我们可以将 @Ben Bolker 的 base36ToInteger 函数应用于我们收集的 id 并将它们与它们的创建日期进行比较:

createData<-data.frame(created=created,ids=sapply(ids,base36ToInteger))
> dput(createData)
structure(list(created = structure(c(1342658844, 1342657298, 
1342622962, 1342643655, 1342641187, 1342654768, 1342665353, 1342640599, 
1342648272, 1342662822, 1342654185, 1342659591, 1342624350, 1342647907, 
1342637587, 1342591960, 1342625515, 1342642330, 1342651384, 1342668363, 
1342608976, 1342608165, 1342632545, 1342638611, 1342643489), class = c("POSIXct", 
"POSIXt")), ids = c(55047001, 55044612, 55010018, 55025557, 55022809, 
55040754, 55056689, 55022221, 55031424, 55053023, 55039810, 55048123, 
55010880, 55030934, 55019343, 54976515, 55011555, 55024060, 55035670, 
55061120, 54998192, 54997264, 55015528, 55020295, 55025363)), .Names = c("created", 
"ids"), row.names = c("wrujd", "wrsp0", "wr202", "wrdzp", "wrbvd", 
"wrppu", "ws20h", "wrbf1", "wriio", "wrz6n", "wrozm", "wrvej", 
"wr2o0", "wri52", "wr973", "wqc5f", "wr36r", "wrcu4", "wrlsm", 
"ws5fk", "wqsvk", "wqs5s", "wr694", "wr9xj", "wrdub"), class = "data.frame")

这意味着随着新帖子的创建,reddit 会在整个网站上按顺序生成这些数字。

如果没有具体的方向,我会留在这里,但希望你能明白。

【讨论】:

  • @ttmaccer,非常感谢!!这比我预期的要多!
【解决方案2】:

我从通用基础转换代码posted by Erich Neuwirth on r-help in 2008 开始:这是递归的,所以可能会很慢——但我花了合适的时间来开发它!

numberInBase <- function(number,base){
    numberInBaseRecur<-function(number,base){
        lastDigit<-function(number,base) number %% base
        if (number == 0) result <- c(0)
        else result <- c(numberInBaseRecur(number %/% base,base),
                         lastDigit(number,base))
        result
    }
    result <- numberInBaseRecur(number,base)
    while (result[1]== 0 && length(result)>1)
        result <- result[-1]
    result
} 

快速测试:

numberInBase(36^3,36)
## [1] 1 0 0 0

现在我们只需要从十进制转换为基数 36,然后索引适当的字母数字字符串。这是您的示例:

b36string <- c(0:9,letters)
paste(b36string[numberInBase(54941875,36)+1],collapse="")
## [1] "wplf7"

如果你需要走另一条路,有一个 post by Jim Holtman from Jan 2012 that gives a solution:

base36ToInteger <- function (Str) {
    common <- chartr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                     , ":;<=>?@ABCDEFGHIJKLMNOPQRS:;<=>?@ABCDEFGHIJKLMNOPQRS"
                     , Str)
    x <- as.numeric(charToRaw(common)) - 48
    sum(x * 36 ^ rev(seq(length(x)) - 1))
} 

base36ToInteger("wplf7")

(我还没有停下来弄清楚这实际上是如何工作的,但是您可以阅读帖子...)

【讨论】:

    猜你喜欢
    • 2010-09-29
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2015-02-08
    • 2012-07-30
    • 2019-09-01
    • 1970-01-01
    • 2015-06-03
    相关资源
    最近更新 更多