【问题标题】:Scraping dl,dt,dd HTML data抓取 dl,dt,dd HTML 数据
【发布时间】:2018-11-26 19:10:42
【问题描述】:

我正在尝试使用 Rvest 和 Selectorgadget 从在线搜索中提取公开可用的房屋描述,并尝试遵循一些在线教程来抓取网络,但我没有得到任何返回。如果有人能指出我正确的方向,将不胜感激!

Site <- "https://paol.snb.ca/paol.html?lang=en&pan=00100004"
snb <- read_html(Site)
snb %>% html_nodes("dd") %>% html_text()

【问题讨论】:

  • 那个网站有很多 JavaScript 事情正在发生(包括使用模式),所以数据可能是动态加载的,而不是 rvest 抓取的源。如果是这样并且允许抓取,则您需要使用更强大的东西,例如 splashr 或 RSelenium 来运行 JavaScript。
  • 感谢 alistair,我自己得出了这个结论,我尝试下载 Rselenium,但由于错误而无法安装:*** arch - x64 错误:'i386' 加载失败。我会在此期间尝试 splashr。

标签: html r web-scraping html-parsing rvest


【解决方案1】:

您不必使用RSelenium。取而代之的是,您可以聪明地使用隐藏的 API,这会更快:

使用 Chrome 中的开发者工具在网络选项卡中获取 API URL:

不要使用原始 URL,而是使用隐藏的 API:https://paol.snb.ca/pas-shim/api/paol/dossier/00100004

library(rvest)
library(httr)
myurl <- "https://paol.snb.ca/pas-shim/api/paol/dossier/00100004"
#you can use any user agent here
ua <- user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36")
my_cookie <- "copy_your_cookie_from_broswer_otherwise_request_will_fail_given_error_no_cookie_available"
my_session <- html_session(myurl,ua,
                         add_headers(Cookie=my_cookie))

result_list <- httr::content(my_session$response,as="parsed") # response is a json string and you will get a list using httr::content

示例结果:

> result_list$summary
$`taxAuth`
[1] "137 - HAUT-MADAWASKA"

$currAsst
[1] 7500

$curLevy
[1] 156.64

$pan
[1] "00100004"

$asstYear
[1] 2018

$imageKey
[1] ""

$description
[1] "Recreational Lot"

$location
[1] "1036 RTE 215"

【讨论】:

  • 感谢 Yifu,我做了 selenium 工作,但可能会尝试使用您的方法改进流程
  • 太好了。 Selenium 易于使用且适合测试。但是如果你想制作一个快速可靠的爬虫,那么最好使用基本的请求。
猜你喜欢
  • 2011-11-27
  • 2020-08-21
  • 1970-01-01
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多