Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用html会话导航到链接_Html_R_Rvest - Fatal编程技术网

在R中使用html会话导航到链接

在R中使用html会话导航到链接,html,r,rvest,Html,R,Rvest,我正在尝试导航到网站上的链接。除了一个链接外,所有链接都可以工作。以下是结果 > mcsession<-html_session("http://www.moneycontrol.com/financials/tataconsultancyservices/balance-sheetVI/TCS#TCS") > mcsession<-mcsession %>% follow_link("Previous Years »") Error: No links have

我正在尝试导航到网站上的链接。除了一个链接外,所有链接都可以工作。以下是结果

> mcsession<-html_session("http://www.moneycontrol.com/financials/tataconsultancyservices/balance-sheetVI/TCS#TCS")

> mcsession<-mcsession %>% follow_link("Previous Years »")
Error: No links have text 'Previous Years »'
In addition: Warning message:
In grepl(i, text, fixed = TRUE) : input string 316 is invalid UTF-8

> mcsession<-mcsession %>% follow_link("Balance Sheet")
Navigating to /financials/tataconsultancyservices/balance-sheetVI/TCS#TCS
Warning message:
In grepl(i, text, fixed = TRUE) : input string 316 is invalid UTF-8
>mcsession mcsession%跟踪链接(“往年”)
错误:没有链接包含“前几年”文本
此外:警告信息:
在grepl(i,text,fixed=TRUE)中:输入字符串316是无效的UTF-8
>mcsession%跟踪链接(“资产负债表”)
导航至/financials/tataconsultancyservices/balance sheetVI/TCS#TCS
警告信息:
在grepl(i,text,fixed=TRUE)中:输入字符串316是无效的UTF-8

你知道为什么会这样吗?

这不是一个普通的链接-它是javascript。我不知道如何使用
rvest
,但您可以使用
RSelenium
,它基本上可以自动化正常的浏览器窗口。它比直接刮削要慢,但是你可以用手把任何事情自动化。这对我很有用(在Windows 10上使用chrome)

库(RSelenium)

rD这不是一个正常的链接-它是javascript。我不知道如何使用
rvest
,但您可以使用
RSelenium
,它基本上可以自动化正常的浏览器窗口。它比直接刮削要慢,但是你可以用手把任何事情自动化。这对我很有用(在Windows 10上使用chrome)

库(RSelenium)

rD如果你查看页面,你会发现你不是针对一个普通的链接。事实上,单击“前几年”不会加载新页面,而是返回一个名为
post\u prevenxt()
的javascript函数。据我所知,
rvest
无法跟随该链接,因为它实际上不是指向其他页面的链接。我认为您最好的选择是使用
RSelenium
以编程方式导航页面,以实现您想要的功能。如果查看页面,您会发现您的目标不是普通链接。事实上,单击“前几年”不会加载新页面,而是返回一个名为
post\u prevenxt()
的javascript函数。据我所知,
rvest
无法跟随该链接,因为它实际上不是指向其他页面的链接。我认为您最好的选择是使用
RSelenium
以编程方式导航页面,以实现您想要的功能。
library(RSelenium)
rD <- rsDriver(port=4444L,browser="chrome")
remDr <- rD$client

remDr$navigate("http://www.moneycontrol.com/financials/tataconsultancyservices/balance-sheetVI/TCS#TCS")

firstpage <- remDr$getPageSource() #you can use this to get the first table

#(1)
webElem1 <- remDr$findElement(using = 'partial link text', value = "Previous Years")
webElem1$clickElement()

nextpage <- remDr$getPageSource() #you can use this to get the next page for previous years

#repeat from #(1) to go back another page etc 

remDr$closeall() #when you have finished.