我想创建一个可以从Trip Advisor中抓取一些数据的爬虫.理想情况下,它将 (a)识别要爬行的所有地点的链接, (b)收集每个地点所有景点的链接, (c)收集所有评论的目的地名称,日期和评级.我现在想集中讨论(a)部分.
这是我开始的网站:http: //www.tripadvisor.co.nz/Tourism-g255104-New_Zealand-Vacations.html
这里有问题:该链接提供了前10个目的地,如果您再点击"查看更多热门目的地",它将展开列表.它似乎使用javascript函数来实现这一点.不幸的是,我不熟悉javascript,但我认为下面的块可能会提供有关它如何工作的线索:
<div class="morePopularCities" onclick="ta.call('ta.servlet.Tourism.showNextChildPage', event, this)">
<img id='lazyload_2067453571_25' height='27' width='27' src='http://e2.tacdn.com/img2/x.gif'/>
See more popular destinations in New Zealand </div>
Run Code Online (Sandbox Code Playgroud)
我已经为R找到了一些有用的网页编写软件包,比如rvest,RSelenium,XML,RCurl,但是其中只有RSelenium似乎能够解决这个问题,尽管如此,我仍然无法使用它出.
这是一些相关的代码:
tu = "http://www.tripadvisor.co.nz/Tourism-g255104-New_Zealand-Vacations.html"
RSelenium::startServer()
remDr = RSelenium::remoteDriver(browserName = "internet explorer")
remDr$open()
remDr$navigate(tu)
# remDr$executeScript("JS_FUNCTION")
Run Code Online (Sandbox Code Playgroud)
最后一行应该在这里做,但我不确定我需要在这里调用什么函数.
一旦我设法扩展这个列表,我将能够以与解决(b)部分相同的方式获取每个目的地的链接,我想我已经解决了这个问题(对于那些感兴趣的人):
library(rvest)
tu = "http://www.tripadvisor.co.nz/Tourism-g255104-New_Zealand-Vacations.html"
tu = html_session(tu)
tu %>% html_nodes(xpath='//div[@class="popularCities"]/a') %>% html_attr("href")
[1] "/Tourism-g255122-Queenstown_Otago_Region_South_Island-Vacations.html"
[2] "/Tourism-g255106-Auckland_North_Island-Vacations.html"
[3] "/Tourism-g255117-Blenheim_Marlborough_Region_South_Island-Vacations.html"
[4] "/Tourism-g255111-Rotorua_Rotorua_District_Bay_of_Plenty_Region_North_Island-Vacations.html"
[5] "/Tourism-g255678-Nelson_Nelson_Tasman_Region_South_Island-Vacations.html"
[6] "/Tourism-g255113-Taupo_Taupo_District_Waikato_Region_North_Island-Vacations.html"
[7] "/Tourism-g255109-Napier_Hawke_s_Bay_Region_North_Island-Vacations.html"
[8] "/Tourism-g612500-Wanaka_Otago_Region_South_Island-Vacations.html"
[9] "/Tourism-g255679-Russell_Bay_of_Islands_Northland_Region_North_Island-Vacations.html"
[10] …Run Code Online (Sandbox Code Playgroud) 我在我的脚本中登录时遇到问题.尽管我在stackoverflow上找到了所有其他好的答案,但没有一个解决方案适合我.
我正在为我的博士研究抓一个网络论坛,其网址是http://forum.axishistory.com.
我想要抓取的网页是成员列表 - 列出所有成员个人资料的链接的页面.如果登录,则只能访问成员列表.如果您尝试在不登录的情况下访问成员列表,则会显示登录表单.
成员列表的URL是:http://forum.axishistory.com/memberlist.php.
我试过httr-package:
library(httr)
members <- GET("http://forum.axishistory.com/memberlist.php", authenticate("username", "password"))
members_html <- html(members)
Run Code Online (Sandbox Code Playgroud)
输出是登录表单.
然后我尝试了RCurl:
library(RCurl)
members_html <- htmlParse(getURL("http://forum.axishistory.com/memberlist.php", userpwd = "username:password"))
members_html
Run Code Online (Sandbox Code Playgroud)
输出是登录表单 - 再次.
然后我尝试了这个主题的list()函数 - 在R中刮掉受密码保护的网站:
handle <- handle("http://forum.axishistory.com/")
path <- "ucp.php?mode=login"
login <- list(
amember_login = "username"
,amember_pass = "password"
,amember_redirect_url =
"http://forum.axishistory.com/memberlist.php"
)
response <- POST(handle = handle, path = path, body = login)
Run Code Online (Sandbox Code Playgroud)
然后再次!输出是登录表单.
我正在研究的下一件事是RSelenium,但经过所有这些尝试,我试图弄清楚我是否可能遗漏了某些东西(可能是完全明显的东西).
我在这里查看了其他相关帖子,但无法弄清楚如何将代码应用于我的案例:
首先,我想为一个新问题道歉,因为我的个人资料还不允许我评论其他人的评论,尤其是我见过的两篇SO帖子.所以请忍受这个老家伙:-)
我试图读取100个字符文件的列表,大小从大约90KB到2MB,然后使用该qdap包进行一些统计,我从文件中提取的文本,即计数句子,单词等.这些文件包含以前使用的网页源RSelenium::remoteDriver$getPageSource()并使用保存到文件write(pgSource, fileName.txt).我正在使用以下循环读取文件:
pgSource <- readChar(file.path(fPath, fileNames[i]), nchars = 1e6)
doc <- read_html(pgSource)
Run Code Online (Sandbox Code Playgroud)
对于某些文件而言是扔的
Error in eval(substitute(expr), envir, enclos) :
Excessive depth in document: 256 use XML_PARSE_HUGE option [1]
Run Code Online (Sandbox Code Playgroud)
我已经看到这些帖子,SO33819103和SO31419409指出类似的问题,但无法完全理解如何使用@blossarch在上面第一个链接中建议的片段中的建议使用@ shabbychef的解决方法.
library(drat)
drat:::add("shabbychef");
install.packages('xml2')
library("xml2")
Run Code Online (Sandbox Code Playgroud)
编辑:我注意到,之前我正在运行另一个脚本使用URL来从网页上实时抓取数据我没有遇到这个问题.代码是一样的,我只是在阅读doc <- read_html(pgSource)后阅读它RSelenium's remoteDriver.
我想问这个温和的社区是我xml2在添加shabbychef的drat之后是否遵循正确的安装和加载步骤,或者我是否需要添加SO17154308帖子中建议的其他步骤.非常感谢任何帮助或建议.谢谢.
R selenium无法到达任何地方.这是第一步和我的输出:
library(RSelenium)
rD <- rsDriver()
# checking Selenium Server versions:
# BEGIN: PREDOWNLOAD
# BEGIN: DOWNLOAD
# BEGIN: POSTDOWNLOAD
# checking chromedriver versions:
# BEGIN: PREDOWNLOAD
# BEGIN: DOWNLOAD
# BEGIN: POSTDOWNLOAD
# checking geckodriver versions:
# BEGIN: PREDOWNLOAD
# BEGIN: DOWNLOAD
# BEGIN: POSTDOWNLOAD
# checking phantomjs versions:
# BEGIN: PREDOWNLOAD
# BEGIN: DOWNLOAD
# BEGIN: POSTDOWNLOAD
# [1] "Connecting to remote server"
# Error in checkError(res) :
# Undefined error in httr call. httr output: Failed to …Run Code Online (Sandbox Code Playgroud) 有没有人从R远程填写网络表格?
我想使用我的分数在R中做一些射箭统计.有一个非常方便的网页,给你分类和障碍http://www.archersmate.co.uk/,我自然希望包括在我的统计表.
是否可以远程填写此表格并将结果返回给R ???
否则我将得到所有差点表并将其自己粘贴到数据库中.
更新:我们已经将问题缩小到这样的事实,即表单提交按钮是用javascript编写的.
我正在使用RSelenium,我想打开并浏览谷歌浏览器.但是,当我想从R打开浏览器时,我总是收到错误.使用以下代码:
library("RSelenium")
startServer()
mybrowser <- remoteDriver(browserName = "chrome")
mybrowser$open()
[1] "Connecting to remote server"
Error: Summary: UnknownError
Detail: An unknown server-side error occurred while processing the command.
class: java.lang.IllegalStateException
Run Code Online (Sandbox Code Playgroud)
相同的代码适用于Firefox.我能做些什么呢?
我将如何RSelenium并行运行?
以下是rvest并行使用的示例
library(RSelenium)
library(rvest)
library(magrittr)
library(foreach)
library(doParallel)
URLsPar <- c("http://www.example.com/", "http://s5.tinypic.com/n392s6_th.jpg", "http://s5.tinypic.com/jl1jex_th.jpg",
"http://s6.tinypic.com/16abj1s_th.jpg", "http://s6.tinypic.com/2ymvpqa_th.jpg")
(detectCores() - 1) %>% makeCluster %>% registerDoParallel
ws <- foreach(x = 1:length(URLsPar), .packages = c("rvest", "magrittr", "RSelenium")) %dopar% {
URLsPar[x] %>% read_html %>% as("character")}
stopImplicitCluster()
Run Code Online (Sandbox Code Playgroud) 我的代码是
library(RSelenium)
library(wdman)
pDrv <- phantomjs(port = 4567L)
Run Code Online (Sandbox Code Playgroud)
以下是运行上述代码后出现的错误。
checking phantomjs versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
Error in if (file.access(phantompath, 1) < 0) { :
argument is of length zero
Run Code Online (Sandbox Code Playgroud)
这个问题困扰了我几个星期,自从我换了笔记本电脑并升级到win10后,它再也不能像以前那样运行良好,请帮助我。
我在@jdharrison上找到了关于如何RSelenium在Windows 上启动Tor的精彩答案:https:
//stackoverflow.com/a/39048970/7837376
RSelenium然而,在新版本中,startServer()它已经rsDriver()不存在了,并且它的替换不会像startServer()之前那样采用java参数.
在新的RSelenium语法中,如何在firefox中启动Tor?首先十分感谢)!
更新到 RStudio 版本 2022.12.0+353 (2022.12.0+353) 后,我无法使用 netstat 找到自由端口。
我以前从未遇到过这个问题。我该如何解决?
library(RSelenium)
library(netstat)
remote_driver <- rsDriver(browser = 'firefox',
verbose = F,
port = free_port())
Error message:
Could not open firefox browser.
Client error message:
Undefined error in httr call. httr output: Failed to connect to localhost port 14415 after 0 ms: Connection refused
Check server log for further details.
Warning message:
In rsDriver(browser = "firefox", verbose = F, netstat::free_port()) :
Could not determine server status.
Run Code Online (Sandbox Code Playgroud)
我努力了:
netstat::free_port()失败了。
我已经把浏览器换成chrome了,没成功。
Docker 从未在我的 MacBook …