0

我试图抓取 sainsburys.co.uk,我正在 R 中运行下一个代码

doc <- htmlTreeParse('http://www.sainsburys.co.uk/shop/gb/groceries/fruit-veg/all-fruit#langId=44&storeId=10151&catalogId=10122&categoryId=12545&parent_category_rn=12518&top_category=12518&pageSize=30&orderBy=FAVOURITES_FIRST&searchTerm')

rootNode <- xmlRoot(doc)

但我有这个错误:

Error in x$children[[1]] : subscript out of bounds

我究竟做错了什么?

4

1 回答 1

1

你可以试试httr图书馆:

library(XML)
library(httr)
url <- 'http://www.sainsburys.co.uk/shop/gb/groceries/fruit-veg/all-fruit#langId=44&storeId=10151&catalogId=10122&categoryId=12545&parent_category_rn=12518&top_category=12518&pageSize=30&orderBy=FAVOURITES_FIRST&searchTerm'
doc <- content(GET(url),type="text/html")
xmlValue(doc["//title"][[1]])
# [1] "All fruit | Sainsbury's"
于 2015-09-08T17:08:13.610 回答