How to scrape a table with rvest and xpath?

爱⌒轻易说出口 提交于 2019-12-06 22:48:40

问题


using the following documentation i have been trying to scrape a series of tables from marketwatch.com

here is the one represented by the code bellow:

The link and xpath are already included in the code:

url <- "http://www.marketwatch.com/investing/stock/IRS/profile"
valuation <- url %>%
  html() %>%
  html_nodes(xpath='//*[@id="maincontent"]/div[2]/div[1]') %>%
  html_table()
valuation <- valuation[[1]]

I get the following error:

Warning message:
'html' is deprecated.
Use 'read_html' instead.
See help("Deprecated") 

Thanks in advance.


回答1:


That website doesn't use an html table, so html_table() can't find anything. It actaully uses div classes column and data lastcolumn.

So you can do something like

url <- "http://www.marketwatch.com/investing/stock/IRS/profile"
valuation_col <- url %>%
    read_html() %>%
    html_nodes(xpath='//*[@class="column"]')

valuation_data <- url %>%
    read_html() %>%
    html_nodes(xpath='//*[@class="data lastcolumn"]')

Or even

url %>%
  read_html() %>%
  html_nodes(xpath='//*[@class="section"]')

To get you most of the way there.

Please also read their terms of use - particularly 3.4.



来源:https://stackoverflow.com/questions/35707534/how-to-scrape-a-table-with-rvest-and-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!