【问题标题】:Use rvest to scrape all p after h? (or other R package)使用 rvest 在 h 之后刮掉所有 p? (或其他 R 包)
【发布时间】:2015-09-10 02:13:20
【问题描述】:

我是 html 抓取领域的新手,在使用 R 中的 rvest 时,我很难在特定标题下提取段落。

我想从具有相对相似设置的多个站点中抓取信息。它们都有相同的标题,但标题下的段落数可以改变。我能够使用以下代码抓取标题下的特定段落:

unitCode <- data.frame(unit = c('SLE010', 'SLE115', 'MAA103'))

html <- sapply(unitCode, function(x) paste("http://www.deakin.edu.au/current-students/courses/unit.php?unit=", 
                                          x,
                                          "&return_to=%2Fcurrent-students%2Fcourses%2Fcourse.php%3Fcourse%3DS323%26version%3D3", 
                                          sep = ''))
assessment <- html[3] %>%
              html() %>%
              html_nodes(xpath='//*[@id="main"]/div/div/p[3]') %>%
              html_text()

“xpath”元素拉入评估标题下的第一段。有些页面在评估标题下有多个段落,如果我更改“xpath”变量以具体指定它们,我可以获得这些段落,例如p[4] 或 p[5]。不幸的是,我想在数百页上迭代这个过程,所以每次都改变 xpath 是不合适的,我什至不知道每一页会有多少段落。

考虑到页面设置的不确定性,我认为在我感兴趣的标题之后拉出所有

是最佳选择。

我想知道是否有一种方法可以在 Assessment 之后使用 rvest 或其他一些 R 抓取包来抓取所有 ?

【问题讨论】:

    标签: html r xpath scrape rvest


    【解决方案1】:

    我仅出于演示目的扩展了它。您应该能够将其应用于您的原始代码。覆盖您最终使用的命名空间中的名称确实不是一个好主意。另请注意,我使用的是最新的(github/devtools 版本)rvest,它使用了xml2 并弃用了html

    密钥是xpath="//h3[contains(., 'Assessment')]/following-sibling::p",因此:

    library(rvest)
    
    unitCode <- data.frame(unit = c('SLE010', 'SLE115', 'MAA103'))
    
    sites <- sapply(unitCode, function(x) paste("http://www.deakin.edu.au/current-students/courses/unit.php?unit=", 
                                              x,
                                              "&return_to=%2Fcurrent-students%2Fcourses%2Fcourse.php%3Fcourse%3DS323%26version%3D3", 
                                              sep = ''))
    
    pg <- read_html(sites[1])
    pg_2 <- read_html(sites[2])
    pg_3 <- read_html(sites[3])
    
    pg %>% html_nodes(xpath="//h3[contains(., 'Assessment')]/following-sibling::p")
    
    ## {xml_nodeset (2)}
    ## [1] <p>This unit is assessed on a pass/fail basis. Multiple-choice on-line test   ...
    ## [2] <p style="margin-top: 2em;">\n  <a href="/current-students/courses/course.php ...
    
    pg_2 %>% html_nodes(xpath="//h3[contains(., 'Assessment')]/following-sibling::p")
    
    ## {xml_nodeset (3)}
    ## [1] <p>Mid-trimester test 20%, three assignments (3 x 10%) 30%, examination 50%.</p>
    ## [2] <p>* Rate for all CSP students, except for those who commenced Education and  ...
    ## [3] <p style="margin-top: 2em;">\n  <a href="/current-students/courses/course.php ...
    
    pg_3 %>% html_nodes(xpath="//h3[contains(., 'Assessment')]/following-sibling::p")
    
    ## {xml_nodeset (6)}
    ## [1] <p>Assessment 1 (Group of 3 students) - Student video presentation (5-7 mins) ...
    ## [2] <p>Assessment 2 (Group of 3 students) - Business plan (3500-4000 words) - 30% ...
    ## [3] <p>Examination (2 hours) - 60%</p>
    ## [4] <p><a href="http://www.deakin.edu.au/glossary?result_1890_result_page=H" targ ...
    ## [5] <p>* Rate for all CSP students, except for those who commenced Education and  ...
    ## [6] <p style="margin-top: 2em;">\n  <a href="/current-students/courses/course.php ...
    

    您也可以使用&lt;p style="margin-top: 2em;"&gt; 作为停止标记。您应该查看xml2as_list 以提供帮助。

    【讨论】:

    • 谢谢@hrbrmstr。这一切都有效,除非我使用read_html() 而不是使用html() 会出错。当我使用 read_html() 并继续运行 html_nodes() 时,我得到 UseMethod("html_nodes") 中的错误:没有适用于 'html_nodes' 的方法应用于类“c('xml_document', 'xml_node')"。当我切换回使用 html() 时,一切正常...我更新了我的 R 版本并重新安装了软件包,所以不确定 read_html() 发生了什么。
    • 我正在使用来自 github 的版本(我应该记住并在帖子中注明)。贬值来了!不过,很高兴它对你有用。
    • 你能举个例子,在上面的解决方案中使用停止标记吗?
    猜你喜欢
    • 2022-09-25
    • 2019-11-18
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2020-04-14
    • 1970-01-01
    • 2019-06-04
    相关资源
    最近更新 更多