【问题标题】:How can I extract multiple items from 1 html using RCrawler's ExtractXpathPat?如何使用 RCrawler 的 ExtractXpathPat 从 1 个 html 中提取多个项目?
【发布时间】:2020-03-02 21:13:38
【问题描述】:

我正在尝试使用 Rcrawler 获取博物馆藏品的标签和数据。我想我在使用 ExtractXpathPat 变量时犯了一个错误,但我不知道如何修复它。

我希望得到这样的输出:

1;"Titel(s)";"De StaalmeestersDe waardijns van het Amsterdamse lakenbereidersgilde, bekend als ‘De Staalmeesters’"
1;"Objecttype";"Schilderij"
1;"Objectnummer";"SK-A-2931"

但是输出文件在第三个位置重复了标题:

1;"Titel(s)";"De StaalmeestersDe waardijns van het Amsterdamse lakenbereidersgilde, bekend als ‘De Staalmeesters’"
1;"Objecttype";"De StaalmeestersDe waardijns van het Amsterdamse lakenbereidersgilde, bekend als ‘De Staalmeesters’"
1;"Objectnummer";"De StaalmeestersDe waardijns van het Amsterdamse lakenbereidersgilde, bekend als ‘De Staalmeesters’"

HTML 如下所示:

<div class="item">
      <h3 class="item-label h4-like">Objectnummer</h3>
      <p class="item-data">SK-A-2931</p>
</div>

我的方法是这样的:

Rcrawler(Website = "https://www.rijksmuseum.nl/nl/", 
         no_cores = 4, no_conn = 4,
         dataUrlfilter = '.*/collectie/.*',
         ExtractXpathPat = c('//*[@class="item-label h4-like"]', '//*[@class="item-data"]'), 
         PatternsNames = c('label','data'),
         ManyPerPattern = TRUE)

目标明确 HTML 页面并不总是具有相同的标签,有时它具有没有相应数据的标签。有时数据在一个段落中,有时在无序列表中。

我的最终目标是创建一个 csv,其中包含网站的所有标签以及每行中的相应数据。

这个问题是收集标签和数据的第一步,然后我将用它们来创建上面提到的 csv。

【问题讨论】:

    标签: r xpath web-crawler rcrawler


    【解决方案1】:

    我不使用 RCrawler 进行抓取,但我认为您的 XPath 需要修复。我为你做了:

    Rcrawler(Website = "https://www.rijksmuseum.nl/nl/", 
             no_cores = 4, no_conn = 4,
             dataUrlfilter = '.*/collectie/.*',
             ExtractXpathPat = c("//h3[@class='item-label h4-like'][.='Titel(s)']/following-sibling::p/text()","//h3[@class='item-label h4-like'][.='Objecttype']/following::a[1]/text()","//h3[@class='item-label h4-like'][.='Objectnummer']/following-sibling::p/text()"), 
             PatternsNames = c("Titel(s)", "Objecttype","Objectnummer"),
             ManyPerPattern = TRUE)
    

    我运行了几分钟,它似乎工作了:

    DATA[[1]]
    $`PageID`
    [1] 1
    
    $`Titel(s)`
    [1] "De Staalmeesters"                                                                   
    [2] "De waardijns van het Amsterdamse lakenbereidersgilde, bekend als ‘De Staalmeesters’"
    
    $Objecttype
    [1] "schilderij"
    
    $Objectnummer
    [1] "SK-C-6"
    

    更多选项:

    蛮力。由于您还不知道所有标签名称,并且如果您不想编写特定的 XPath,您可以在 RCrawlers ExtractXpathPat 中尝试这样的操作:

    c("string((//h3[@class='item-label h4-like'])[1]/parent::*)","string((//h3[@class='item-label h4-like'])[2]/parent::*)",...,"string((//h3[@class='item-label h4-like'])[30]/parent::*)")
    

    在这里,我们只是从位置 1 递增到位置 30。您可以尝试 40,50,这取决于您。

    PatternsNames = c("Item1", "Item2",...,"Item30")

    结果示例:

    Item1:Title(s) The Seven Works of MercyPolyptych with the Seven Works of Charity 
    Item2:Object type painting 
    Item3:Object number SK-A-2815
    ...
    Item17:Parts The Seven Works of Mercy (SK-A-2815-1) The Seven Works of Mercy (SK-A-2815-2) The Seven Works of Mercy (SK-A-2815-3) The Seven Works of Mercy (SK-A-2815-4) The Seven Works of Mercy (SK-A-2815-5) The Seven Works of Mercy (SK-A-2815-6) The Seven Works of Mercy (SK-A-2815-7)
    ...
    Item29:
    Item30:
    

    然后您需要使用适当的工具(dplyr、stringr)整理数据(拆分、修剪、重组...)以生成适当的 csv。

    如果此选项不起作用,您可以确定您可能拥有的所有标签名称(获取网页的所有 //h3[@class='item-label h4-like']/text() 并删除重复项以仅保留唯一值。然后相应地编写 Xpath。这样.csv 会更容易生成。

    您还可以在 RCrawler 之外工作(使用其他工具)并编写一些函数来正确抓取数据(使用应用函数或 for 循环)。

    【讨论】:

    • 我需要获取所有带有标签的项目数据,而不是每个页面都有相同的数据。有没有办法创建一个通用 xpath 来完成您的解决方案的工作?
    • 我已经更新了我的答案。约 675 000 件需要刮擦的作品。那是一条很长的路。除非你只对绘画感兴趣。
    • 这似乎可行,虽然我还没有完成所有 675000
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2018-01-20
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多