【问题标题】:How do I use the rvcontinue parameter in the Mediawiki API using R?如何使用 R 在 Mediawiki API 中使用 rvcontinue 参数?
【发布时间】:2022-11-14 06:13:16
【问题描述】:

我试图提取数百页的维基百科修订历史。但是,Mediawiki API 将任何给定页面的返回限制设置为 500 (https://www.mediawiki.org/wiki/API:Revisions)。

“rvcontinue”参数允许您提取下一个 500 等等,但我不确定如何在 R 中自动执行此操作。(我看过一些 Python 代码示例 (Why does the Wikipedia API Call in Python throw up a Type Error?),但我没有知道如何在 R 中复制它)。

下面附加了一个页面的示例 GET 请求代码,感谢任何帮助!

base_url <- "http://en.wikipedia.org/w/api.php"

query_param <- list(action  = "query",
                    pageids = "8091", 
                    format = "json",
                    prop = "revisions",
                    rvprop = "timestamp|ids|user|userid|size",
                    rvlimit = "max",
                    rvstart = "2014-05-01T12:00:00Z",
                    rvend = "2021-12-30T23:59:00Z",
                    rvdir = "newer",
                    rvcontinue = #the continue value returned from the original request goes here                       
)

revision_hist <- GET(base_url, query_param)

理想情况下,我的 GET 请求会每 500 个值自动更新 rvcontinue 参数,直到没有剩余。

谢谢!

【问题讨论】:

    标签: r mediawiki-api


    【解决方案1】:

    我还没有完全解决它,但我注意到您可能在http::GET() 中缺少query = query_param。在这里,我尝试使用rvcontinue = "rvcontinue",但现在似乎不起作用。

    base_url <- "http://en.wikipedia.org/w/api.php"
    
    query_param <- list(action  = "query",
                        pageids = "8091", 
                        format = "json",
                        prop = "revisions",
                        rvprop = "timestamp|ids|user|userid|size",
                        rvlimit = "max",
                        rvstart = "2014-05-01T12:00:00Z",
                        rvend = "2021-12-30T23:59:00Z",
                        rvdir = "newer",
                        rvcontinue = "rvcontinue"                       
    )
    
    response <- httr::GET(base_url, query = query_param)
    
    parsed <- jsonlite::fromJSON(httr::content(response, as = "text"))
    

    这是我的错误信息:

    > print(parsed)
    $error
    $error$code
    [1] "badcontinue"
    
    $error$info
    [1] "Invalid continue param. You should pass the original value returned by the previous query."
    
    $error$`*`
    [1] "See https://en.wikipedia.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/postorius/lists/mediawiki-api-announce.lists.wikimedia.org/&gt; for notice of API deprecations and breaking changes."
    
    
    $servedby
    [1] "mw1398"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多