使用Rvest进行网页抓取:不兼容的尺寸错误

时间:2017-07-24 02:59:05

标签: r rvest

Rstudio 版本1.0.143并使用 Rvest 包。

我正在尝试从网页抓取数据但是我收到错误:

Error in bind_rows_(x, .id) : incompatible sizes (20 != 9)

我认为由于某些变量没有数据,因此其行数比其他变量短。你能否证实这个假设是正确的以及如何纠正它。有没有办法为该行没有数据写入NA,以便所有变量具有相同的行号。

以下是我的代码。

domain_web <- ("https://www.domain.com.au/sold-listings/?area=blue-mountains-surrounds-nsw,canterbury-bankstown-nsw,eastern-suburbs-nsw,hawkesbury-nsw,hills-nsw,inner-west-nsw,liverpool-fairfield-nsw,macarthur-camden-nsw,northern-beaches-nsw,northern-suburbs-nsw,north-shore-lower-nsw,north-shore-upper-nsw,parramatta-nsw,st-george-nsw,sutherland-nsw,sydney-city-nsw,western-sydney-nsw&sort=suburb-asc&page=%d")


map_df(1:50, function(i) {
  cat(".")

  pg <- read_html(sprintf(domain_web, i))

  list(Date = html_text(html_nodes(pg, ".is-sold")),
             Price = html_text(html_nodes(pg, ".listing-result__price")),
             Address = html_text(html_nodes(pg, ".listing-result__address-line-1 span")),
             Suburb = html_text(html_nodes(pg, ".listing-result__address-line-2 span")),
             Bedroom = html_text(html_nodes(pg, ".listing-result__feature-bed")),
             Bathroom = html_text(html_nodes(pg, ".listing-result__feature-bathroom")),
             Garage = html_text(html_nodes(pg, ".listing-result__feature-parking")), stringsAsFactors = F)}) ->Property_price

我已经为日期和价格分别运行了以上命令 - 它可以工作,但是当包含其他变量时,我收到上面提到的错误代码。

非常感谢你的帮助。

由于

0 个答案:

没有答案