从R中的网页顺序检索数据

时间:2013-04-25 16:49:03

标签: xml r web xml-parsing

我在网络上进行了高级搜索并获得了一些结果。对于每个结果,我有兴趣提取2个字段,“Referencia:”和“CIF”。

#This is the url with the results of the search
url="http://www.boe.es/buscar/boe.php?campo%5B1%5D=DOC&dato%5B1%5D=edicto+auto+declaracion+concurso+CIF
&campo%5B6%5D=FPU&dato%5B6%5D%5B0%5D=25%2F04%2F2013&dato%5B6%5D%5B1%5D=30%2F04%2F2013
&sort_field%5B0%5D=fpu&sort_order%5B0%5D=desc&sort_field%5B1%5D=ref&sort_order%5B1%5D=asc&accion=Buscar"

#This is the url of one of the results.
example=http://www.boe.es/buscar/doc.php?id=BOE-B-2013-15895

CIF字段通常为X00000000或X-00000000格式X=c("A","B")0=0:9  和Referencia领域是示例中的BOE-B-2013-15895和CIF B-32210196

你能帮我从R吗?

2 个答案:

答案 0 :(得分:1)

要获取内容,请查看httr包。你可以使用像

这样的东西
content (GET (url))

答案 1 :(得分:1)

1)获得Referencia

是一块蛋糕
substrRight <- function(x, n){
  sapply(x, function(xx)
  substr(xx, (nchar(xx)-n+1), nchar(xx)))
}

library(XML)
u<-"http://www.boe.es/buscar/boe.php?campo%5B1%5D=DOC&dato%5B1%5D=edicto+auto+declaracion+concurso+CIF%20&campo%5B6%5D=FPU&dato%5B6%5D%5B0%5D=25%2F04%2F2013&dato%5B6%5D%5B1%5D=30%2F04%2F2013%20&sort_field%5B0%5D=fpu&sort_order%5B0%5D=desc&sort_field%5B1%5D=ref&sort_order%5B1%5D=asc&accion=Buscar" #link
doc1<-htmlParse(u) 'get html'
kbbRoot <- xmlRoot(doc1) #parse it into xml
els<-getNodeSet(kbbRoot,"//*[contains(concat( ' ', @class, ' ' ), concat( ' ', 'resultado-busqueda-link-defecto', ' ' ))]") #get all links by xpath
links<-sapply(els, function(el) xmlGetAttr(el, "href")) #get inner (start with .../)
links<-sapply(links, function(x)  substr(x,start=3,stop=nchar(x))) #delete ../  
links<-sapply(links, function(x)  paste("http://www.boe.es", x,sep=""))#generate correct link
Referencia<-sapply(links, function(x) substrRight(x,16)) # get referencia from links

2)CIF要复杂得多。你必须使用正则表达式。不幸的是,我并不坚强。所以在论坛上问别人:“应该使用正则表达式从字符串中获取CIF值吗?”

CIFRA<-function (u){
  doc1<-htmlParse(u)#get html
  kbbRoot <- xmlRoot(doc1)# parse it
  els<-getNodeSet(kbbRoot,"//*[contains(concat('', @class,''), concat('', 'parrafo', '' ))]")#select text
  l<-sapply(els, xmlValue) #analyse each sentences
  x<-regexpr(pattern="[A-Z][0-9]+",text=l)#Try to find CIF by using RegEXP
  #regexp return position in string
  ind<-which.max(x) #'number of row with CIF'
  st<- x[ind]-3 #start position
  en<-st+attr(x, "match.length")[ind]-1 #finish
  res<-substring(l[ind],st,en) #select text between start and finish
}  

CIF&lt; -sapply(links,function(x)CIFRA(x))

相关问题