rvest:从网页上删除表格

时间:2018-02-23 11:21:48

标签: r rvest

我正在尝试检索下表:

可在this website.

找到

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

password_verify

使用以下结果(仅前3行):

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)

我设法做了类似的事情,使用 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" 来检索博彩公司的名称。

我的问题是:有没有办法一次性废弃这张桌子?

1 个答案:

答案 0 :(得分: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以查找我们正在追踪的数据。

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

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

    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调用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

相关问题