
R爬蟲之京東商城手機信息批量獲取
人手一部智能手機的移動互聯網時代,智能手機對很多人來說,它就像我們身上生長出來的一個器官那樣重要。如果你不能對各大品牌的『賣點』和『受眾』侃上一陣,很可能會被懷疑不是地球人。
今天我們來探索一下,如何從『京東商城』爬取各大品牌的手機信息。
1.預備知識
R爬蟲需要掌握的技能包括:
基本的網頁知識,如html,XML文件的解析
分析XPath
使用網頁開發工具
異常捕捉的處理
字符串的處理
正則表達式的使用
數據庫的基本操作
不過不要擔心,目前只需要掌握前三項技能,即可開始練習。
前三項技能的掌握可以參考 Automated Data Collection with R 一書。正常情況下,一天之內大致即可掌握。
2.頁面分析
(待完善)
3.提取各大品牌的鏈接
#### packages we need ####
## ----------------------------------------------------------------------- ##
require(stringr)
require(XML)
require(RCurl)
library(Rwebdriver)
setwd("JDDownload")
BaseUrl<-"http://search.jd.com"
quit_session()
start_session(root = "http://localhost:4444/wd/hub/",browser = "firefox")
# post Base Url
post.url(url = BaseUrl)
SearchField<-element_xpath_find(value = '//*[@id="keyword"]')
SearchButton<-element_xpath_find(value = '//*[@id="gwd_360buy"]/body/div[2]/form/input[3]')
#keyword for search
keywords<-'手機'
element_click(SearchField)
keys(keywords)
element_click(SearchButton)
Sys.sleep(1)
#test
get.url()
pageSource<-page_source()
parsedSourcePage<-htmlParse(pageSource, encoding = 'UTF-8')
## Download Search Results
fname <- paste0(keywords, " SearchPage 1.html")
writeLines(pageSource, fname)
#get all the brand url
Brand<-'//*[@id="J_selector"]/div[1]/div/div[2]/div[3]/ul/li/a/@href'
BrandLinks<-xpathSApply(doc = parsedSourcePage, path = Brand)
View(data.frame(BrandLinks))
BrandLinks<-sapply(BrandLinks,function(x){
paste0(BaseUrl,"/",x)
})
save(BrandLinks,file = 'BrandLinks.rda')
4.訪問每個品牌的頁面,抓取每個品牌下的商品鏈接
##############Function 1 #################################3##
### 對各品牌的手機頁面進行抓取 ########3#
getBrandPage<-function(BrandUrl,foreDownload = T){
#獲取某品牌搜索頁面
post.url(BrandUrl)
Brand_pageSource<-page_source()
#parse
parsedSourcePage<-htmlParse(Brand_pageSource, encoding = 'UTF-8')
#get brand name
BrandNamePath<-'//*[@id="J_crumbsBar"]/div[2]/div/a/em'
BrandName<-xpathSApply(doc = parsedSourcePage, path = BrandNamePath, fun = xmlValue)
#Save the page
BrandPageName<-paste0(BrandName,'_PageSource.html')
#Create a file
if(!file.exists(BrandName)) dir.create(BrandName)
# save
writeLines(text = Brand_pageSource, con = paste0(BrandName,'/',BrandPageName))
# get the product page url
#path
Brand_AllProductPath<-'//*[@id="J_goodsList"]/ul/li/div/div[4]/a/@href'
#url
Brand_AllProductLinks<-xpathSApply(doc = parsedSourcePage, path = Brand_AllProductPath)
# #remove some false url
# FalseLink<-grep(x = Brand_AllProductLinks,pattern = 'https',fixed = TRUE)
# Brand_AllProductLinks<-Brand_AllProductLinks[-FalseLink]
# add a head
Brand_AllProductLinks<-str_c('http:',Brand_AllProductLinks)
#save and return the url
save(Brand_AllProductLinks,file = paste0(BrandName,'_AllProductLinks.rda'))
return(Brand_AllProductLinks)
}
# test
BrandUrl<-BrandLinks[1]
getBrandPage(BrandUrl)
#get all the links
Brand_ProductLink<-list()
for(i in 1:length(BrandLinks)){
Sys.sleep(10)
Brand_ProductLink[[i]]<-getBrandPage(BrandUrl = BrandLinks[i])
}
#clean the links
All_ProductLink<-lapply(Brand_ProductLink,function(x){
TrueLink<-grep(x = x,pattern = 'http://item.jd.com/',fixed = TRUE,value = FALSE)
return(x[TrueLink])
})
# save the links
save(All_ProductLink,file = 'All_ProductLink.rda')
5.訪問每個商品頁面,提取有用信息
我們初步提取如下指標:標題(Title),賣點(KeyCount),價格(Price),評論數(commentCount),尺寸(Size),后置攝像頭像素(BackBit),后置攝像頭像素(ForwardBit),核數(Core),分辨率(Resolution),品牌(Brand),上架時間(onSaleTime).
#################################################
######## Function2 :訪問每個商品頁面,提取有用信息 ########
Product<-function(ProductLink){
post.url(ProductLink)
Sys.sleep(4)
# get the page
Product_pageSource<-page_source()
#parse
Parsed_product_Page<-htmlParse(Product_pageSource, encoding = 'UTF-8')
# get title,,key count,price,CommentCount and so on
#PATH
TitlePath<-'//*[@id="name"]/h1'
KeyCountPath<-'//*[@id="p-ad"]'
PricePath<-'//*[@id="jd-price"]'
commentCountPath<-'//*[@id="comment-count"]/a'
SizePath<-'//*[@id="parameter1"]/li[1]/div/p[1]'
BackBitPath<-'//*[@id="parameter1"]/li[2]/div/p[1]'
ForwardBitPath<-'//*[@id="parameter1"]/li[2]/div/p[2]'
CorePath<-'//*[@id="parameter1"]/li[3]/div/p[1]'
NamePath<-'//*[@id="parameter2"]/li[1]'
CodePath<-'//*[@id="parameter2"]/li[2]'
BrandPath<-'//*[@id="parameter2"]/li[3]'
onSaleTimePath<-'//*[@id="parameter2"]/li[4]'
ResolutionPath<-'//*[@id="parameter1"]/li[1]/div/p[2]'
Title<-xpathSApply(doc = Parsed_product_Page,path = TitlePath,xmlValue)
KeyCount<-xpathSApply(doc = Parsed_product_Page,path = KeyCountPath,xmlValue)
Price<-xpathSApply(doc = Parsed_product_Page,path = PricePath,xmlValue)
commentCount<-xpathSApply(doc = Parsed_product_Page,path = commentCountPath,xmlValue)
Size<-xpathSApply(doc = Parsed_product_Page,path = SizePath,xmlValue)
BackBit<-xpathSApply(doc = Parsed_product_Page,path = BackBitPath,xmlValue)
ForwardBit<-xpathSApply(doc = Parsed_product_Page,path = ForwardBitPath,xmlValue)
Core<-xpathSApply(doc = Parsed_product_Page,path = CorePath,xmlValue)
Name<-xpathSApply(doc = Parsed_product_Page,path = NamePath,xmlValue)
Code<-xpathSApply(doc = Parsed_product_Page,path = CodePath,xmlValue)
Resolution<-xpathSApply(doc = Parsed_product_Page,path = ResolutionPath,xmlValue)
Brand<-xpathSApply(doc = Parsed_product_Page,path = BrandPath,xmlValue)
onSaleTime<-xpathSApply(doc = Parsed_product_Page,path = onSaleTimePath,xmlValue)
# 整理成data frame
mydata<-data.frame(Title = Title,KeyCount = KeyCount, Price = Price,
commentCount = commentCount, Size = Size, BackBit = BackBit,
ForwardBit = ForwardBit, Core = Core, Name = Name,Code = Code,
Resolution = Resolution,
Brand = Brand, onSaleTime = onSaleTime)
#save the page
FileName<-paste0('Product/',Brand,Code,'_pageSource.html')
writeLines(text = Product_pageSource,con = FileName)
#return the data
return(mydata)
}
# test
quit_session()
start_session(root = "http://localhost:4444/wd/hub/",browser = "firefox")
load(file = 'All_ProductLink.rda')
ProductLink1<-All_ProductLink[[40]][1]
testData<-Product(ProductLink = ProductLink1)
#定義tryCatch
mySpider<-function(ProductLink){
out<-tryCatch(
{
message('This is the try part:')
Product(ProductLink = ProductLink)
},
error=function(e){
message(e)
return(NA)
},
finally = {
message("The end!")
}
)
return(out)
}
## loop
# get all data
ProductInformation<-list()
k <-0
for(i in 1:length(All_ProductLink)){
for(j in 1:length(All_ProductLink[[i]])){
k<-k+1
ProductInformation[[k]]<-mySpider(ProductLink = All_ProductLink[[i]][j])
}
}
# save my data
MobilePhoneInformation<-do.call(rbind,ProductInformation)
View(MobilePhoneInformation)
save(MobilePhoneInformation,file = 'MobilePhoneInformation.rda')
nrow(na.omit(MobilePhoneInformation))
View(MobilePhoneInformation)
最終,獲得800多行的信息,除去缺失值,剩下600多行數據,還不賴。 最后的數據可以在這里獲得。
不過,數據還需要進一步清洗方能進行分析。
數據分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
CDA數據分析師證書考試體系(更新于2025年05月22日)
2025-05-26解碼數據基因:從數字敏感度到邏輯思維 每當看到超市貨架上商品的排列變化,你是否會聯想到背后的銷售數據波動?三年前在零售行 ...
2025-05-23在本文中,我們將探討 AI 為何能夠加速數據分析、如何在每個步驟中實現數據分析自動化以及使用哪些工具。 數據分析中的AI是什么 ...
2025-05-20當數據遇見人生:我的第一個分析項目 記得三年前接手第一個數據分析項目時,我面對Excel里密密麻麻的銷售數據手足無措。那些跳動 ...
2025-05-20在數字化運營的時代,企業每天都在產生海量數據:用戶點擊行為、商品銷售記錄、廣告投放反饋…… 這些數據就像散落的拼圖,而相 ...
2025-05-19在當今數字化營銷時代,小紅書作為國內領先的社交電商平臺,其銷售數據蘊含著巨大的商業價值。通過對小紅書銷售數據的深入分析, ...
2025-05-16Excel作為最常用的數據分析工具,有沒有什么工具可以幫助我們快速地使用excel表格,只要輕松幾步甚至輸入幾項指令就能搞定呢? ...
2025-05-15數據,如同無形的燃料,驅動著現代社會的運轉。從全球互聯網用戶每天產生的2.5億TB數據,到制造業的傳感器、金融交易 ...
2025-05-15大數據是什么_數據分析師培訓 其實,現在的大數據指的并不僅僅是海量數據,更準確而言是對大數據分析的方法。傳統的數 ...
2025-05-14CDA持證人簡介: 萬木,CDA L1持證人,某電商中廠BI工程師 ,5年數據經驗1年BI內訓師,高級數據分析師,擁有豐富的行業經驗。 ...
2025-05-13CDA持證人簡介: 王明月 ,CDA 數據分析師二級持證人,2年數據產品工作經驗,管理學博士在讀。 學習入口:https://edu.cda.cn/g ...
2025-05-12CDA持證人簡介: 楊貞璽 ,CDA一級持證人,鄭州大學情報學碩士研究生,某上市公司數據分析師。 學習入口:https://edu.cda.cn/g ...
2025-05-09CDA持證人簡介 程靖 CDA會員大咖,暢銷書《小白學產品》作者,13年頂級互聯網公司產品經理相關經驗,曾在百度、美團、阿里等 ...
2025-05-07相信很多做數據分析的小伙伴,都接到過一些高階的數據分析需求,實現的過程需要用到一些數據獲取,數據清洗轉換,建模方法等,這 ...
2025-05-06以下的文章內容來源于劉靜老師的專欄,如果您想閱讀專欄《10大業務分析模型突破業務瓶頸》,點擊下方鏈接 https://edu.cda.cn/g ...
2025-04-30CDA持證人簡介: 邱立峰 CDA 數據分析師二級持證人,數字化轉型專家,數據治理專家,高級數據分析師,擁有豐富的行業經驗。 ...
2025-04-29CDA持證人簡介: 程靖 CDA會員大咖,暢銷書《小白學產品》作者,13年頂級互聯網公司產品經理相關經驗,曾在百度,美團,阿里等 ...
2025-04-28CDA持證人簡介: 居瑜 ,CDA一級持證人國企財務經理,13年財務管理運營經驗,在數據分析就業和實踐經驗方面有著豐富的積累和經 ...
2025-04-27數據分析在當今信息時代發揮著重要作用。單因素方差分析(One-Way ANOVA)是一種關鍵的統計方法,用于比較三個或更多獨立樣本組 ...
2025-04-25CDA持證人簡介: 居瑜 ,CDA一級持證人國企財務經理,13年財務管理運營經驗,在數據分析就業和實踐經驗方面有著豐富的積累和經 ...
2025-04-25