#先贴一下总的代码,后面有重点!
# https://www.jianshu.com/p/f768cbf5ee0c
# 判断网页编码
library(rvest)
f <- tempfile()#创建临时文件
download.file("https://list.tmall.com/search_product.htm?q=%B5%CDGI&type=p&vmarket=&spm=a2156.1676643.a2227oh.d100&from=mallfp..pc_1_searchbutton", f)
fchars <- readChar(f, file.info(f)$size)
stringi::stri_enc_detect(fchars)
# 发现文档是 GB18030 编码,上面那个方法就是这个编码问题
futf8 <- stringi::stri_encode(fchars, "GB18030", "UTF8")
fhtml <- rvest::html(futf8)
#以上全为重新为网页编码的过程,此法可通用
# 天猫抓取数据
# GBK
library(rvest)
library(magrittr)
library(xml2)
#保存需要爬取的网页到hfurl
hfurl<- "https://list.tmall.com/search_product.htm?q=%B5%CDGI&type=p&vmarket=&spm=a2156.1676643.a2227oh.d100&from=mallfp..pc_1_searchbutton"
SCdata <- hfurl %>%
read_html(encoding="GBK")%>% #读取hfurl的链接并指定编码为"GBK"
html_nodes("div.product-iWra