【发布时间】:2019-04-15 12:10:54
【问题描述】:
我正在尝试从 OTC Markets 收集一些数据(在他们的 robots.txt 范围内),但我无法连接到网页。
- 我尝试的第一步只是从页面上抓取 HTML,但页面需要加载 javascript。
- 所以我下载了 phantomjs 并以这种方式连接。但是,这会导致 404 错误页面
- 然后我将用户代理更改为类似于用户的东西,看看它是否能让我连接,但还是没有运气!这是怎么回事
这是我的代码的可重现版本,我们将不胜感激。 Phantomjs 可以在这里下载:http://phantomjs.org/
library(rvest)
library(xml2)
library(V8)
# example website, I have no correlation to this stock
url <- 'https://www.otcmarkets.com/stock/YTROF/profile'
# create javascript file that phantomjs can process
writeLines(sprintf("var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.open('%s', function () {
console.log(page.content); //page source
phantom.exit();
});", url), con="scrape.js")
html <- system("phantomjs.exe_PATH scrape.js", intern = TRUE)
page_html <- read_html(html)
【问题讨论】:
-
backend.otcmarkets.com/otcapi/stock/trade/inside/… && backend.otcmarkets.com/otcapi/company/profile/full/… && backend.otcmarkets.com/otcapi/stock/trade/inside/… (开发工具是你的好朋友;它们返回可爱的 JSON;参数看起来很合理,并且可以与其他股票重复;即你不需要任何可怕的幻影) + ????用于检查??????
-
哇!我不知道这甚至是一种选择。感谢您的帮助,我是否应该保留原件以解决此问题? @hrbrmstr
-
完全由你决定。这是 SO 上经常发生的事情(尤其是在 R 标签中)。出于某种原因,人们还没有接受异步 XHR 请求。
标签: r web-scraping phantomjs