【问题标题】:Scraping Javascript in R with RSelenium使用 RSelenium 在 R 中抓取 Javascript
【发布时间】:2015-08-02 18:26:55
【问题描述】:

我正在尝试抓取Washington Post's database on police shootings。由于不是 html 我不能使用rvest,所以我使用了RSeleniumphantomjs

library(RSelenium)

checkForServer()
startServer()

eCap <- list(phantomjs.binary.path = "C:/Program Files/Chrome Driver/phantomjs.exe")
remDr <- remoteDriver(browserName = "phantomjs", extraCapabilities = eCap)

remDr$open()

remDr$navigate("http://www.washingtonpost.com/graphics/national/police-shootings/")

在查看源代码后,很明显我感兴趣的项目有以下idclass

<div id="js-list-690" class="listWrapper cf">

或在 Chrome 中:

我可以访问单个项目的文本:

remDr$findElement("css", "#js-list-691")$getElementText()

返回

[[1]]
[1] "An unidentified person, a 47-year-old Hispanic man, was shocked with a stun gun and shot on July 30, 2015, in Whittier, Calif. Los Angeles County deputies were investigating a domestic disturbance when he threatened the officers and struck one of them with a metal rod.\nMALEDEADLY WEAPONHISPANIC45 TO 54\nCBS Los AngelesWhittier Daily News"}

但如果我想获得所有这些项目的列表:

remDr$findElements("class name", "listWrapper cf")

导致错误。

我该怎么做

  1. 获取共享此listWrapper cf 类的所有元素的列表?
  2. 返回与每个元素关联的文本列表?

【问题讨论】:

标签: r selenium selenium-webdriver web-scraping rselenium


【解决方案1】:

直接使用 JSON 数据会更容易方式(使用几乎所有现代浏览器中的“开发者工具”来跟踪加载的 URL...这并不需要很长时间在该列表中查找):

library(jsonlite)

url <- "https://js.washingtonpost.com/graphics/policeshootings/policeshootings.json?d14385542"

shootings <- fromJSON(url)

dplyr::glimpse(shootings)

## Observations: 564
## Variables:
## $ id                 (int) 3, 4, 5, 8, 9, 11, 13, 15, 16, 17, 19, 21, ...
## $ date               (chr) "2015-01-02", "2015-01-02", "2015-01-03", "...
## $ description        (chr) "Elliot, who was on medication for depressi...
## $ blurb              (chr) "a 53-year-old man of Asian heritage armed ...
## $ name               (chr) "Tim Elliot", "Lewis Lee Lembke", "John Pau...
## $ age                (int) 53, 47, 23, 32, 39, 18, 22, 35, 34, 47, 25,...
## $ gender             (chr) "M", "M", "M", "M", "M", "M", "M", "M", "F"...
## $ race               (chr) "A", "W", "H", "W", "H", "W", "H", "W", "W"...
## $ armed              (chr) "gun", "gun", "unarmed", "toy weapon", "nai...
## $ city               (chr) "Shelton", "Aloha", "Wichita", "San Francis...
## $ state              (chr) "WA", "OR", "KS", "CA", "CO", "OK", "AZ", "...
## $ address            (chr) "600 block of E. Island Lake Drive", "4519 ...
## $ lat                (dbl) 47.24683, 45.48620, 37.69477, 37.76291, 40....
## $ lon                (dbl) -123.12159, -122.89128, -97.28055, -122.422...
## $ is_geocoding_exact (lgl) TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, T...
## $ mental             (lgl) TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FAL...
## $ sources            (list) http://kbkw.com/local-news/329755, http://...
## $ photos             (list) NULL, NULL, 107, , , , //img.washingtonpos...
## $ videos             (list) NULL, NULL, NULL, NULL, NULL, NULL, NULL, ...

【讨论】:

  • @hrbrmstr--谢谢!这是太棒了。我将保持问题的开放性,看看是否可以使用 Selenium 找到更通用的解决方案,但这肯定会解决这个挑战。
  • 嗨,这个答案看起来可能对我自己的项目有很大帮助。您能否详细解释一下“在几乎所有现代浏览器中使用“开发人员工具”来跟踪加载的 URL” 部分,或者请给我指点在线指南或教程?
  • @user3198404 不是专家,但... 使用 Chrome:右键单击 > 检查;导航到 Network 选项卡 > 输入 .json > 搜索 > 刷新站点(捕捉之前的呼叫)
  • 你能看看这个问题吗? stackoverflow.com/questions/66996370/… 谢谢!
猜你喜欢
  • 2016-06-06
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多