2016-11-04 60 views
0

我使用rvest从谷歌查询结果中检索标题。我的代码是这样的:如何从查询结果中检索标题使用rvest

> url = URLencode(paste0("https://www.google.com.au/search?q=","600d")) 
> page <- read_html(url) 
> page %>% 
    html_nodes("a") %>% 
    html_text() 

但是,结果不仅包括刚冠军,但还有其他方面,如:

[24] "Past month"                   
[25] "Past year"                   
[26] "Verbatim"                    
[27] "EOS 600D - Canon"                 
[28] "Similar"                   
[29] "Canon 600D | BIG W"                 
[30] "Cached"                     
[31] "Similar" 
...... 
[45] ""                       
[46] ""      

哪里还有什么我需要的是[27]“EOS 600D - 佳能“和[29]”佳能600D | BIG W“。他们显示在谷歌查询是这样的:enter image description here

所有其他人只是对我来说噪音。任何人都可以请告诉我如何摆脱这些?

另外,如果我还需要描述部分,我该怎么办?

+1

值得一读:http://stackoverflow.com/a/22703153/5977215 – SymbolixAU

+0

刮谷歌是违反他们的服务条款,你也要求其他人违反这个问题。 – hrbrmstr

+0

非常感谢。我会尝试另一种方式 –

回答

2

只得到了冠军,不使用<a>(=链接),但<h3>

page %>% 
    html_nodes("h3") %>% 
    html_text() 

[1] "EOS 600D - Canon"             
[2] "Canon EOS 600D - Wikipedia"           
[3] "Canon 600D | BIG W"             
[4] "Canon EOS 600D Digital SLR Camera with 18-55mm IS Lens Kit ..."  
[5] "Canon Rebel T3i/EOS 600D Review: Digital Photography Review"  
[6] "Canon EOS 600D review - CNET"          
[7] "canon eos 600d | Cameras | Gumtree Australia Free Local Classifieds" 
[8] "Images for 600d"              
[9] "Canon 600D - Snapsort"            
[10] "Canon EOS 600D - Georges Cameras" 
+0

哇!这真太了不起了 !非常感谢! –