Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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 使用xml2删除web表的前两列_R_Xml2 - Fatal编程技术网

R 使用xml2删除web表的前两列

R 使用xml2删除web表的前两列,r,xml2,R,Xml2,我一直在努力在R中使用xml包,我需要一些帮助来使用xml2删除一些格式良好的表 我要抓取的表的第一页的url是 . 在某些页面上,我需要第二个和第三个表,但在其他页面上,我需要第一个和第二个表。一个常见的线索是,我希望所有的表中的'caption'标记包含文本'that meet',并存储在一个列表中,而表格中的'caption'标记包含文本'that not meet any'。但我真的不知道该怎么做。我正在使用的代码如下所示。我可以想象,一定有某种排序方法可以使regexp成为选择整个表的

我一直在努力在R中使用xml包,我需要一些帮助来使用xml2删除一些格式良好的表

我要抓取的表的第一页的url是 . 在某些页面上,我需要第二个和第三个表,但在其他页面上,我需要第一个和第二个表。一个常见的线索是,我希望所有的表中的'caption'标记包含文本'that meet',并存储在一个列表中,而表格中的'caption'标记包含文本'that not meet any'。但我真的不知道该怎么做。我正在使用的代码如下所示。我可以想象,一定有某种排序方法可以使regexp成为选择整个表的条件。希望代码能起作用

#Define urls
urls<-lapply(seq(1,12, 1), function(x) paste('http://www.chemicalsubstanceschimiques.gc.ca/challenge-defi/batch-lot-',x,'/index-eng.php', sep=''))
#scrap the text
batches<-lapply(urls, function(x) read_html(x))
#Return the tables from each 
batches_tables<-lapply(batches, function(x) xml_find_all(x, './/table'))
#get the table from the first
out<-batches[[1]]
#Inspect
out[[1]] #do not want this table
out[[2]] #want this table pasted in one list, caption='that meet'
out[[2]] #want this table pasted in a second list, caption='that do not meet'
#定义URL

URL使用
contains()
caption
标记作为目标,然后向上移动到父项:

library(xml2)
library(rvest)

URL <- "http://www.chemicalsubstanceschimiques.gc.ca/challenge-defi/batch-lot-1/index-eng.php#s1"
pg <- read_html(URL)

html_nodes(pg, xpath=".//table/caption[contains(., 'that meet')]/..")
## {xml_nodeset (1)}
## [1] <table class="fontSize80">&#13;\n          <caption>&#13;\n          ...

html_nodes(pg, xpath=".//table/caption[contains(., 'that do not meet')]/..")
## {xml_nodeset (1)}
## [1] <table class="fontSize85">&#13;\n          <caption>&#13;\n          ...
库(xml2)
图书馆(rvest)

URL
rvest
有一个非常好的SelectorGadget,它可以帮助您找到相关的CSS或XPath选择器。请参阅
vignette(“selectorgadget”,package='rvest')
<代码>rvest::html_表
在这里也很有用。