R中的jsonlite包出错

时间:2016-12-01 16:17:22

标签: html json r web web-scraping

尝试网页抓取网站时有没有人收到此错误:

Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :
lexical error: invalid char in json text.
                                     <!doctype html><html xmlns="htt
                   (right here) ------^

我不明白为什么我在第一页抓取网站的第一页没有问题时收到此错误。但第二页我得到了这个错误。有办法解决这个问题吗?

这很好用:

    jsonlite::fromJSON("https://www.reddit.com/r/BestOfStreamingVideo/.json", flatten = TRUE)

在此处获取错误:

jsonlite::fromJSON("https://www.reddit.com/r/BestOfStreamingVideo/?count=25&after=t3_5fvgls/.json", flatten = TRUE)

1 个答案:

答案 0 :(得分:1)

后者不会返回JSON。它返回HTML。在浏览器中输入两个URL,您将看到差异。

我猜您要查找的网址是:

https://www.reddit.com/r/BestOfStreamingVideo/.json?count=25&after=t3_5fvgls/

您需要先放置./json并在之后附加网址参数。