2014-09-22 120 views
2

從該網站上,http://www.lewisthomason.com/locations/這個HTML代碼的部分有我想提取,即如何在公司的辦公室位於四個會市(諾克斯維爾,孟菲斯,納什維爾和塞維爾)的XPath 1.0表達式返回NULL

<div id="the_content"> 
<div class="one_fourth"> 
<h3> 
<cufon class="cufon cufon-canvas" alt="KNOXVILLE" style="width: 87px; height: 26px;"> 
<canvas width="104" height="25" style="width: 104px; height: 25px; top: -1px; left: 0px;"></canvas> 
<cufontext>KNOXVILLE</cufontext> 
</cufon> 
</h3> 
<p> 
<h6> 
</div> 
<div class="one_fourth"> 
<div class="one_fourth"> 
<div class="one_fourth last"> 
<div class="clearboth"></div> 
<p></p> 
</div> 
</div> 
<div id="secondary"> </div> 
<div class="clearboth"></div> 
</div> 

我嘗試了這些XPath搜索

require(XML) 
require(httr) 
doc <- content(GET('http://www.lewisthomason.com/locations/')) 

xpathSApply(doc, "//div[@id = 'the_content']/div//p", xmlValue, trim = TRUE) 
xpathSApply(doc, "//div[@class = 'one_fourth']//p", xmlValue, trim = TRUE) 

我得到的都是空的幾個變化。什麼樣的表達會帶回城市名稱或整個地址?我知道第四個城市,所以我會修改最後的表達。

感謝您的任何指導。

回答

3

rvest救援通過CSS選擇器(XPath的WLD以及工作)如果你給它一個合適的用戶代理它會送你正確的內容:

library(rvest) # for scraping 
library(httr) # only for user_agent() 

pg <- html_session("http://www.lewisthomason.com/locations/", 
        user_agent("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")) 

# get names 
pg %>% html_nodes("h3") %>% html_text() 

## [1] "KNOXVILLE" "MEMPHIS"  "NASHVILLE" "SEVIERVILLE" 

# get locations 
pg %>% html_nodes("h3~p") %>% html_text() %>% .[1:4] 

## [1] "One Centre Square, Fifth Floor\n620 Market Street\nPO Box 2425\nKnoxville, TN 37901\nPhone (865) 546-4646\nFax (865) 523-6529" 
## [2] "40 S Main St #2900\nMemphis, TN 38103\nPhone (901) 525-8721\nFax (901) 525-6722"            
## [3] "424 Church Street, Suite 2500\nPO Box 198615\nNashville, TN 37219\nPhone (615) 259-1366\nFax (615) 259-1389"     
## [4] "248 Bruce St, Suite 2\nSevierville, TN 37862\nPhone (865) 429-1999\nFax (865) 428-1612" 
+0

的包裝紙來包裝的包裝;) – jdharrison 2014-09-22 14:22:22

+0

的確:-)雖然這應該使它更容易爲人們獲取數據,尤其與'SelectorGadget'書籤包括哈德利在小插曲。它非常適合整個新的「管道」時尚。 – hrbrmstr 2014-09-22 14:25:02

+0

順便提一下,從magrittr進口%>%,所以你不需要dplyr – hadley 2014-09-23 17:32:59

3

該網站正在檢查用戶代理。

require(XML) 
require(RCurl) 
myAgent <- "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0" 
doc <- getURL('http://www.lewisthomason.com/locations/', useragent = myAgent) 
doc <- htmlParse(doc) 


> xpathSApply(doc, "//div[@id = 'the_content']/div//p", xmlValue, trim = TRUE) 
[1] "One Centre Square, Fifth Floor\n620 Market Street\nPO Box 2425\nKnoxville, TN 37901\nPhone (865) 546-4646\nFax (865) 523-6529" 
[2] "40 S Main St #2900\nMemphis, TN 38103\nPhone (901) 525-8721\nFax (901) 525-6722"            
[3] "424 Church Street, Suite 2500\nPO Box 198615\nNashville, TN 37219\nPhone (615) 259-1366\nFax (615) 259-1389"     
[4] "248 Bruce St, Suite 2\nSevierville, TN 37862\nPhone (865) 429-1999\nFax (865) 428-1612"          
[5] ""                                
> xpathSApply(doc, "//div[@class = 'one_fourth']//p", xmlValue, trim = TRUE) 
[1] "One Centre Square, Fifth Floor\n620 Market Street\nPO Box 2425\nKnoxville, TN 37901\nPhone (865) 546-4646\nFax (865) 523-6529" 
[2] "40 S Main St #2900\nMemphis, TN 38103\nPhone (901) 525-8721\nFax (901) 525-6722"            
[3] "424 Church Street, Suite 2500\nPO Box 198615\nNashville, TN 37219\nPhone (615) 259-1366\nFax (615) 259-1389" 

否則將被髮送:

> getURL('http://www.lewisthomason.com/locations/') 
[1] "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>403 Forbidden</title>\n</head><body>\n<h1>Forbidden</h1>\n<p>You don't have permission to access /locations/\non this server.</p>\n</body></html>\n"