来自rvest的html_form并不承认形式

时间:2016-11-21 15:34:16

标签: html r web-scraping rvest

我试图用rvest链接的论文/摘要,只是数字,标题,作者等)来抓取this website的内容。< / p>

默认情况下,该页面仅显示2016年论文,并且抓取2016年数据“没有问题”。我希望在更改&#34; 2016&#34;之后URL会发生变化到了&#34;所有年份&#34;,但它仍然是相同的。所以我使用了html_form。在检查&#34;资源&#34;在网页上,我发现相关的输入名称为filteryear

R-代码

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

显然,filteryear不是表格的一部分。由于我拥有的HTML知识有限,我很确定下面告诉我,该表单包含三个输入:filterTypeNamefilterNamefilteryear

来自资源的HTML:

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

为什么html_form无法完全识别此表单?更重要的是,有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为html_form可以正常工作,但您可以手动简单地httr::POST表单,如下所示:

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)

我猜第7张桌子是你想要的:

> 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