【问题标题】:rvest: Scraping table from webpagervest:从网页中抓取表格
【发布时间】:2018-02-23 11:21:47
【问题描述】:

我正在尝试检索下表:

this website.上找到

我设法使用以下代码检索了引号:

library('rvest')
url.2 <- "https://www.wettportal.com/Fussball/Champions_League/Champions_League/Paris_Saint-Germain_-_Real_Madrid_2448367.html"
webpage.2 <- read_html(url.2)
oddscell.html <- html_nodes(webpage.2, ".oddscell")
oddscell.data <- html_text(oddscell.html)
home <- oddscell.data[seq(1, length(oddscell.data), 3)]
draw <- oddscell.data[seq(2, length(oddscell.data), 3)]
away <- oddscell.data[seq(3, length(oddscell.data), 3)]

my.quotes <- cbind(home, draw, away)

结果如下(仅前 3 行):

  my.quotes[1:3,]
  home                     draw                    away                   
 [1,] "1.67"                   "4.25"                  "4.35"                 
 [2,] "1.68"                   "4.10"                  "4.20"                 
 [3,] "1.72"                   "4.70"                  "4.56"                 
         

我设法使用html_nodes(webpage.2, ".bookie") 来检索博彩公司的名称。

我的问题是:有没有办法一次刮桌子?

【问题讨论】:

  • 喜欢html_table(html_nodes(webpage.2, ".table-type-odds-1")) ?
  • @MartinSchmelzer 正是我想要的,vielen Dank :)

标签: r rvest


【解决方案1】:

那个网站被我屏蔽了!我在那里什么都看不到,但我可以告诉你,基本上,应该是这样的。

html_nodes() 函数将每个 HTML 标记转换为 R 数据框中的一行。

library(rvest)

## Loading required package: xml2

# Define the url once.
URL <- "https://scistarter.com/finder?phrase=&lat=&lng=&activity=At%20the%20beach&topic=&search_filters=&search_audience=&page=1#view-projects"

    scistarter_html <- read_html(URL)
    scistarter_html

## {xml_document}
## <html class="no-js" lang="en">
## [1] <head>\n<meta http-equiv="Content-Type" content="text/html; charset= ...
## [2] <body>\n    \n    \n    <svg style="position: absolute; width: 0; he ...

我们能够检索在浏览器中看到的相同 HTML 代码。这还没有用,但它确实表明我们能够检索我们在浏览器中看到的相同 HTML 代码。现在我们将开始过滤 HTML 以找到我们想要的数据。

我们想要的数据存储在一个表格中,我们可以通过查看“检查元素”窗口来判断。

这会抓取所有包含链接的节点。

    scistarter_html %>%
      html_nodes("a") %>%
      head()

## {xml_nodeset (6)}
## [1] <a href="/index.html" class="site-header__branding" title="go to the ...
## [2] <a href="/dashboard">My Account</a>
## [3] <a href="/finder" class="is-active">Project Finder</a>
## [4] <a href="/events">Event Finder</a>
## [5] <a href="/people-finder">People Finder</a>
## [6] <a href="#dialog-login" rel="modal:open">log in</a>

在一个更复杂的示例中,我们可以使用它来“抓取”页面,但那是另一天的事了。

页面上的每个 div:

    scistarter_html %>%
      html_nodes("div") %>%
      head()

## {xml_nodeset (6)}
## [1] <div class="site-header__nav js-hamburger b-utility">\n        <butt ...
## [2] <div class="site-header__nav__body js-hamburger__body">\n          < ...
## [3] <div class="nav-tools">\n            <div class="nav-tools__search"> ...
## [4] <div class="nav-tools__search">\n              <div class="field">\n ...
## [5] <div class="field">\n                <form method="get" action="/fin ...
## [6] <div class="input-group input-group--flush">\n                    <d ...

... 导航工具 div。这由 css where class=nav-tools 调用。

    scistarter_html %>%
      html_nodes("div.nav-tools") %>%
      head()

## {xml_nodeset (1)}
## [1] <div class="nav-tools">\n            <div class="nav-tools__search"> ...

我们可以通过id来调用节点,如下所示。

    scistarter_html %>%
      html_nodes("div#project-listing") %>%
      head()

## {xml_nodeset (1)}
## [1] <div id="project-listing" class="subtabContent">\n          \n       ...

所有表格如下:

    scistarter_html %>%
      html_nodes("table") %>%
      head()

## {xml_nodeset (6)}
## [1] <table class="table-project-2-col u-mb-0">\n<legend class="u-visuall ...
## [2] <table class="table-project-2-col u-mb-0">\n<legend class="u-visuall ...
## [3] <table class="table-project-2-col u-mb-0">\n<legend class="u-visuall ...
## [4] <table class="table-project-2-col u-mb-0">\n<legend class="u-visuall ...
## [5] <table class="table-project-2-col u-mb-0">\n<legend class="u-visuall ...
## [6] <table class="table-project-2-col u-mb-0">\n<legend class="u-visuall ...

有关更多信息,请参阅下面的(相关)链接。

https://rpubs.com/Radcliffe/superbowl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2019-02-17
    • 1970-01-01
    • 2017-04-01
    相关资源
    最近更新 更多