【问题标题】:Rcrawler scrape does not yield pagesRcrawler 抓取不产生页面
【发布时间】:2018-07-31 12:40:58
【问题描述】:

我正在使用 Rcrawler 提取维基百科页面的信息框。我有一个音乐家列表,我想提取他们的姓名、出生日期、死亡日期、乐器、标签等。然后我想将列表中所有艺术家的数据框创建为行并将数据存储为列/向量。

下面的代码没有抛出任何错误,但我也没有得到任何结果。代码中使用的xpath在我单独使用rvest时有效。

我的代码有什么问题?

library(Rcrawler)
jazzlist<-c("Art Pepper","Horace Silver","Art Blakey","Philly Joe Jones")

Rcrawler(Website = "http://en.wikipedia.org/wiki/Special:Search/", no_cores = 4, no_conn = 4, 
     KeywordsFilter = jazzlist,
     ExtractXpathPat = c("//th","//tr[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]//td",
                         "//tr[(((count(preceding-sibling::*) + 1) = 6) and parent::*)]//td"),
     PatternsNames = c("artist", "dob", "dod"), 
     ManyPerPattern = TRUE, MaxDepth=1 )

【问题讨论】:

    标签: r web-scraping rcrawler


    【解决方案1】:

    从特定的维基百科 URL 列表中抓取数据

    如果您想抓取具有常见模式的特定 URL 列表,请使用 ContentScraper 函数:

    library(Rcrawler)
    jazzlist<-c("Art Pepper","Horace Silver","Art Blakey","Philly Joe Jones")
    target_pages = paste0('https://en.wikipedia.org/wiki/Special:Search/', gsub(" ", "_", jazzlist))
    DATA<-ContentScraper(Url = target_pages , 
                         XpathPatterns = c("//th","//tr[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]//td","//tr[(((count(preceding-sibling::*) + 1) = 6) and parent::*)]//td"),
                         PatternsName = c("artist", "dob", "dod"),
                         asDataFrame = TRUE)
    View(DATA)
    

    从维基百科的链接列表中抓取数据

    通过一点努力,我在维基百科中找到了一个硬波普音乐家列表,我想你会对抓取所有这些艺术家的数据感兴趣;在这种情况下,我们将使用 Rcrawler 函数自动收集和解析所有这些页面。

    Rcrawler(Website = "https://en.wikipedia.org/wiki/List_of_hard_bop_musicians" ,
             no_cores = 4, no_conn = 4, MaxDepth = 1, 
             ExtractXpathPat = c("//th","//tr[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]//td","//tr[(((count(preceding-sibling::*) + 1) = 6) and parent::*)]//td"),
             PatternsNames = c("artist", "dob", "dod"),
             crawlZoneXPath = "//*[@class='mw-parser-output']")
    #transform data into dataframe
     df<-data.frame(do.call("rbind", DATA))
    
    • MaxDepth = 1:仅抓取起始页中的链接
    • crawlZoneXPath : 仅抓取页面正文中的链接(艺术家列表)
    • ExtractXpathPat:要提取的数据的 XPath 模式

    爬虫创建者

    【讨论】:

    • 对于MaxDepth,您的意思是它只会抓取搜索的第一页,例如,如果您在 google 中搜索某些内容并且每页只有 10 个结果,还是它会抓取所有页面?抱歉,如果这令人困惑
    【解决方案2】:

    我可能是错的,但我怀疑你认为 Rcrawler 包的工作方式与它的工作方式不同。您可能会将抓取与抓取混淆。

    Rcrawler 只是从给定页面开始并从该页面爬出任何链接。您可以使用 URL 过滤器或关键字过滤器缩小路径,但仍需要通过爬网过程到达这些页面。它不运行搜索。

    您从 Wikipedia 搜索页面开始这一事实表明您可能期望它根据您在 jazzlist 中指定的字词运行搜索,但它不会这样做。它只会跟随维基百科搜索页面中的所有链接,例如左侧边栏中的“主页”、“内容”、“特色内容”,它最终可能会或可能不会遇到您使用过的术语之一,在这种情况下,它将根据您的 xpath 参数抓取数据。

    您指定的术语将非常少见,因此虽然最终可能会通过文章交叉链接(例如“精选页面”)找到它们,但需要很长时间。

    我认为你想要的是根本不使用 Rcrawler,而是在搜索词的循环中调用 rvest 函数。您只需将这些术语附加到您提到的搜索 URL 中,并用下划线替换空格:

    library(rvest)
    target_pages = paste0('https://en.wikipedia.org/wiki/Special:Search/', gsub(" ", "_", jazzlist))
    
    for (url in target_pages){
        webpage = read_html(url)
        # do whatever else you want here with rvest functions 
    }
    

    编辑:根据他的评论,在下面添加了 OP 针对他的具体案例的确切代码的解决方案

    library(rvest)
    target_pages = paste0('https://en.wikipedia.org/wiki/Special:Search/', gsub(" ", "_", jazzlist))
    
    for (url in target_pages){
        webpage = read_html(url)
        info<-webpage %>% html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "plainlist", " " ))]') %>% html_text() temp<-data.frame(info, stringsAsFactors = FALSE) data = rbind(data,temp) 
    }
    

    【讨论】:

    • 我在 #dowhat else 评论之后添加了以下内容:info&lt;-webpage %&gt;% html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "plainlist", " " ))]') %&gt;% html_text() temp&lt;-data.frame(info, stringsAsFactors = FALSE) data = rbind(data,temp) 这有效并创建了一个数据框,其中包含准备好用正则表达式解析的字符数据。
    • @Ben 很高兴听到它有效,在这种情况下您能否将答案标记为正确。
    • 当然@Mike-s 只是想知道协议是否要将我的附加代码添加到您的原始答案中? :) 什么是最好的?
    • @Ben 当然,我已将其添加到答案中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多