Ken S. Ken S. - 13 days ago 11
R Question

html_form from rvest doesn't recognise form

I am trying to scrape the content of this website with

rvest
(not the linked papers/abstracts, just the number, title, authors, etc.).

Per default, the page displays 2016 papers only and scraping the 2016 data was 'no problem'. I was hoping the URL would change after changing "2016" to "all years", but it remains the same. So I resorted to
html_form
. Upon inspecting "resources" of the webpage, I found the relevant input name to be
filteryear
.

R-code:

library(rvest)
rdc <- html_session("https://sfb649.wiwi.hu-berlin.de/fedc/discussionPapers_formular_content.php")
form <- html_form(rdc)
form <- set_values(form, filteryear = "all years")
#Error: Unknown field names: filteryear


So apparently,
filteryear
is not part of the form. With the limited HTML-knowledge I have, I am pretty sure the below tells me, that the form consists of three inputs:
filterTypeName
,
filterName
and
filteryear
.

HTML from resource:

<form action='discussionPapers_formular_content.php' method='post'>
<select name='filterTypeName'>
<option value='AUTHORS'>Author</option>
<option value='PROJECT'>Project Code</option>
...
<option value='JEL'>JEL</option
</select> </td> # Is this </td> the problem?!
<td valign='baseline'>
<input type='text' size='35' name='filterName' >
</td>
<td valign='baseline'>
<select name='filteryear'>
<option value='2005'>2005</option>
...
<option value='2016'>2016</option>
<option value='all'>all years</option>
</select>
</td>
<td valign='baseline'>
&nbsp;&nbsp;<INPUT type='submit' value='Search' name='B1'></INPUT>
</td></tr>
</form>


Why is
html_form
not recognising this form completely? And, more importantly, is there a way to solve this problem?

Answer

I count bring html_form to work but you can simply httr::POST the form manually as follows:

library(rvest)
library(httr)
res <- POST("https://sfb649.wiwi.hu-berlin.de/fedc/discussionPapers_formular_content.php",
     body = list(filterTypeName = "filterTypeName:AUTHORS",
                 filteryear = "all",
                 B1 = "Search"), encode = "form")
out <- read_html(res) %>% html_table(fill=TRUE)

I guess the 7th table is the one you want:

> dim(out[[7]])
[1] 805  10

> head(out[[7]])

        X1                                                                                     X2
1 2016-049                                                                              Q3-D3-LSA
2 2016-048                                     Unraveling of Cooperation in Dynamic Collaboration
3 2016-047                                                            Time Varying Quantile Lasso
4 2016-046                                                           Credit Rating Score Analysis
5 2016-045                                          Information Acquisition and Liquidity Dry-Ups
6 2016-044 Dynamic Contracting with Long-Term Consequences: Optimal CEO Compensation and Turnover
                                                                  X3  X4         X5                       X6
1                               Lukas Borke and   Wolfgang K. Härdle  B1 15.11.2016        C87,   C88,   G17
2                                                        Suvi Vasama  A8 07.11.2016        C73,   D83,   O31
3      Lenka Zbonakova,  Wolfgang Karl H\177ardle and   Weining Wang  B1 07.11.2016 C21,   G01,   G20,   G32
4 Wolfgang Karl H\177ärdle,  Phoon Kok Fai and   David Lee Kuo Chuen  B1 02.11.2016 C01,   G00,   G17,   G24
5                                 Philipp Koenig and   David Pothier C10 26.10.2016        D82,   G01,   G12
6                                                        Suvi Vasama  A8 26.10.2016        C73,   D82,   D86
  X7 X8 X9 X10
1 NA NA NA  NA
2 NA NA NA  NA
3 NA NA NA  NA
4 NA NA NA  NA
5 NA NA NA  NA
6 NA NA NA  NA
Comments