2014-08-27 36 views
0

我想获得的价格,并从下面的网址蒸汽网站游戏名称的列表,但无法弄清楚如何xpathSApplyshould解析下面:R代表webscraping - 拉价格和名称

http://store.steampowered.com/search/?sort_by=Price & SORT_ORDER = ASC &放大器;';“>价格

这里是我的代码

require(RCurl) 
require(XML) 
url <- "http://store.steampowered.com/search/results?sort_by=Name&sort_order=ASC&category1=1" 
SOURCE <- getURL(url,encoding="UTF-8") #Download the page 
substring (SOURCE,1,200) 
PARSED <- htmlParse(SOURCE) #Format the html code 
##My problem is in this line below 
(xpathSApply(PARSED, "//div[@class='col search_price']")) 

回答

3

试试这个:

require(RCurl) 
require(XML) 
url <- "http://store.steampowered.com/search/?sort_by=Metascore&sort_order=DESC&" 
SOURCE <- getURL(url, encoding="UTF-8") #Download the page 
PARSED <- htmlParse(SOURCE, asText = TRUE, encoding = "utf-8") 
xpaths <- c(price="//a/div[@class='col search_price']", 
      title="//div[@class='col search_name ellipsis']/h4") 
res <- sapply(xpaths, function(x) xpathSApply(PARSED, x, xmlValue, trim = TRUE)) 
head(res) 
#  price title       
# [1,] "9,99€" "Half-Life 2"     
# [2,] "9,99€" "Half-Life"     
# [3,] "19,99€" "BioShock™"     
# [4,] "18,99€" "The Orange Box"    
# [5,] "19,99€" "Portal 2"     
# [6,] "14,99€" "The Elder Scrolls V: Skyrim" 
+0

很好的答案,尤其是创建标头的技巧。我使用此方法:PARSED < - htmlTreeParse(url,useInternal = TRUE)而不是RCurl plus getURL()加上htmlParse。有什么不同? – lawyeR 2014-08-28 01:59:43

+0

谢谢@lawyeR。 Afaik,'htmlParse'只是'htmlTreeParse(useInternalNodes = TRUE,...)'的快捷方式。我离开了OP的'RCurl',因为它在需要的时候给了你更大的抓取控制。 – lukeA 2014-08-28 09:00:05