###1 抓取的網址是360團購html
http://tuan.360.cn/bei_jing/c_0.html?kw=電影&pageno=1#tuanFilterweb
###2 利用firefox的FireBug插件分析其源代碼,以下所示:數據庫
"//*/h3[@class='desc']" 匹配電影院名稱 "//*/span [@class='discount']" 匹配原價 "//*/span [@class='price']" 匹配優惠價 "//*/div [@class='other-info clearfix']" 匹配備註信息 "//*/div[@class='source clearfix']" 匹配團購信息來源
###3 源代碼app
## 利用RCurl抓取電影團購信息 library(RCurl) library(XML) library("plyr") page <- 1:5 urlist[page] <- paste("http://tuan.360.cn/bei_jing/c_0.html?kw=電影&pageno=",page,"#tuanFilter",sep="") #僞造報頭 myheader=c("User-Agent"="Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) ", "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language"="en-us", "Connection"="keep-alive", "Accept-Charset"="GB2312,utf-8;q=0.7,*;q=0.7" ) #電影院名稱 dyy_name<-c("") #原價 last_price<-c("") #優惠價格 now_price<-c("") #備註 others<-c("") #來源 tg_source<-c("") for(url in urlist){ #下載網址 webpage <- getURL(url,httpheader=myheader,.encoding="utf-8") #轉化成XML格式 pagetree <- htmlTreeParse(webpage,encoding="utf-8", error=function(...){}, useInternalNodes = TRUE,trim=TRUE) #利用XPATH篩選 temp_name <- xpathSApply (pagetree, "//*/h3[@class='desc']", xmlValue) dyy_name<-c(dyy_name,temp_name) temp_price <- xpathSApply (pagetree, "//*/span [@class='discount']",xmlValue) last_price<-c(last_price,temp_price) temp_now_price <- xpathSApply (pagetree, "//*/span [@class='price']",xmlValue) now_price<-c(now_price,temp_now_price) temp_others <- xpathSApply (pagetree, "//*/div [@class='other-info clearfix']",xmlValue) others<-c(others,temp_others) temp_tg_source <- xpathSApply (pagetree, "//*/div[@class='source clearfix']", xmlValue) tg_source<-c(tg_source,temp_tg_source) } # 刪除沒必要要的信息 tg_source<-laply(as.list(tg_source),function(x){ unlist(strsplit(x,"\n"))[2] }) # 刪除沒必要要的信息 others<-laply(as.list(others),function(x){ unlist(strsplit(x,"\n"))[2] }) #組裝成數據庫 content<-data.frame(dyy_name,last_price,now_price,others,tg_source) names(content)<-c('影院','原價','優惠價','備註','來源') #寫入csv文件 write.csv(content,file="電影團購信息.csv")