使用R语言软件(Rstudio)抓取天猫商品数据示例 2020-11-27

本教程演示如何利用R语言(Rstudio)进行天猫商品数据的抓取,通过提供的代码实例,详解抓取过程中的关键步骤。
摘要由CSDN通过智能技术生成

 #先贴一下总的代码,后面有重点!

# 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值