如何使用R网页抓取点击信息?

Mar*_*ski 5 r web-scraping rvest

我正试图从这个网站上删除电话号码:http://olx.pl/oferta/pokoj-1-os-bielany-encyklopedyczna-CID3-IDdX6wf.html#c1c0e14c53.可以使用rvest带选择器的包来抓取电话号码.\'id_raw\'\::nth-child(1) span+ div strong(由selectorGadget建议).

问题是在点击掩码后可以获得信息.所以我不得不打开一个会话,提供一个点击,然后抓取信息.

编辑顺便说一下,它不是一个链接imho.看看来源.我有一个问题,因为我是一个普通的R用户,而不是一个javascript程序员.

在此输入图像描述

hrb*_*str 6

您可以抓取<li>标记中嵌入的数据,告诉onclick处理程序要做什么,直接获取数据:

library(httr)
library(rvest)
library(purrr)
library(stringr)

URL <- "http://olx.pl/oferta/pokoj-1-os-bielany-encyklopedyczna-CID3-IDdX6wf.html#c1c0e14c53"

pg <- read_html(URL)

html_nodes(pg, "li.rel") %>%       # get the 'special' <li> tags
  html_attrs() %>%                 # extract all the attrs (they're non-standard)
  flatten_chr() %>%                # list to character vector
  keep(~grepl("rel \\{", .x)) %>%  # only want ones with 'hidden' secret data
  str_extract("(\\{.*\\})") %>%    # only get the data
  unique() %>%                     # there are duplicates
  map_df(function(x) {

    path <- str_match(x, "'path':'([[:alnum:]]+)'")[,2]                  # extract out the path
    id <- str_match(x, "'id':'([[:alnum:]]+)'")[,2]                      # extract out the id

    ajax <- sprintf("http://olx.pl/ajax/misc/contact/%s/%s/", path, id)  # make the AJAX/XHR URL
    value <- content(GET(ajax))$value                                    # get the data

    data.frame(path=path, id=id, value=value, stringsAsFactors=FALSE)    # make a data frame

  }) 

## Source: local data frame [3 x 3]
## 
##           path    id       value
##          (chr) (chr)       (chr)
## 1        phone dX6wf 503 155 744
## 2        skype dX6wf    e.bobruk
## 3 communicator dX6wf     7686136
Run Code Online (Sandbox Code Playgroud)

完成所有这些后,我非常失望,网站没有更好的服务/使用条款.很明显,他们真的不希望你抓取这些数据.