使用rvest抓取h之后的所有p? (或其他 R 包)

问题描述 投票:0回答:1

我是 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 是不合适的,而且我什至不知道每个页面中有多少段落。

我认为考虑到页面设置的不确定性,在我感兴趣的标题后面拉出所有< p >是最好的选择。

我想知道是否有办法使用 rvest 或其他 R 抓取包在 < p >Assessment< h3 > 之后抓取所有 < h3 > ?

html r web-scraping xpath rvest
1个回答
9
投票

我扩展它只是为了演示目的。您应该能够将其应用到您的原始代码中。覆盖您最终使用的命名空间中的名称确实不是一个好主意。另请注意,我正在使用最新的(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 ...

您也可以使用

<p style="margin-top: 2em;">
作为停止标记。您应该查看
xml2
as_list
来寻求帮助。

© www.soinside.com 2019 - 2024. All rights reserved.