Как скрести следующую таблицу в R? - PullRequest
0 голосов
/ 28 апреля 2020

Мне нужно поцарапать в R таблицу внизу страницы по следующей ссылке:

https://www.macrotrends.net/stocks/charts/azo/autozone/pe-ratio

1 Ответ

3 голосов
/ 28 апреля 2020

Использование rvest

Очистка веб-части

library(rvest)
webpage <- read_html("https://www.macrotrends.net/stocks/charts/azo/autozone/pe-ratio")
html <- rvest::html_nodes(webpage, "thead+ thead th , #style-1 td")
results <- rvest::html_text(html)

Разделение столбцов, поскольку results это просто список

Date <- results[seq(5, length(results), 4)]
`Stock Price` <- results[seq(6, length(results), 4)]
`TTM Net EPS` <- results[seq(7, length(results), 4)]
`PE Ratio` <- results[seq(8, length(results), 4)]
results <- data.frame(Date, `Stock Price`, `TTM Net EPS`, `PE Ratio`, stringsAsFactors = FALSE)

Посмотрите

head(results)
        Date Stock.Price TTM.Net.EPS PE.Ratio
1 2020-04-27     1060.52                16.25
2 2020-02-29     1032.51      $65.27    15.82
3 2019-11-30     1177.92      $64.37    18.30
4 2019-08-31     1101.69      $63.54    17.34
5 2019-05-31     1027.11      $55.97    18.35
6 2019-02-28      938.97      $53.40    17.58
...