用rvest从HTML表中提取超链接

时间:2017-05-12 06:50:18

标签: html r web-scraping href rvest

我在这里看到过类似的问题并实施了解决方案,但似乎仍无法解决这个问题。还是一个R新手,所以请耐心等待:我已经设法使用rvest从this website获得了巴拉克·奥巴马的演讲表:

library(rvest)
page <- read_html("http://www.americanrhetoric.com/barackobamaspeeches.htm")
speeches <- page %>%
  html_nodes(xpath = '//*[@id="AutoNumber1"]') %>% 
  html_table(fill=TRUE)
speeches <- speeches[[1]][,2:4]
head(speeches)

产生:

            X2                                            X3    X4
1             <NA>                                          <NA>  <NA>
2    Delivery Date                  Speech Title/Text/MultiMedia Audio
3     27 July 2004 Democratic National Convention Keynote Speech   mp3
4  06 January 2005 Senate Speech on Ohio Electoral Vote Counting   mp3
5     04 June 2005              Knox College Commencement Speech   mp3
6 15 December 2005              Senate Speech on the PATRIOT Act   mp3

但是,我还想在“语音”列中提取每个条目的超链接,该列自然存在于href属性中。我在网上对此进行了彻底的研究,有些人说要用html_attr('href')指定html属性,但是如果我在上面的代码中包含它,我会收到此错误:

Error in UseMethod("xml_attr") : no applicable method for 'xml_attr' applied to an object of class "list"

另一个人建议用trace来修补实际的功能,但这似乎过于涉及一些看起来很简单的事情。知道我在哪里绊倒吗?

0 个答案:

没有答案