my code is down like this, it can tell me the new book price and I can run this code in R from up to down but I can't run it outside like use
library(rvest)
library(data.table)
library(dplyr)
#这里是导入网址。研究一下amazon的顺序,直接导入就好
id<-1:5
url_increase_fast<-paste0(
"
http://www.amazon.cn/gp/movers-and-shakers/digital-text/ref=zg_bsms_digital-text_pg_",
id,
"?ie=UTF8&pg=",
id)
url_newest<-paste0(
"
http://www.amazon.cn/gp/new-releases/digital-text/ref=zg_bsnr_digital-text_pg_",
id,
"?ie=UTF8&pg=",
id)
url<-c(url_increase_fast,url_newest)
#这里编写readdata函数,读取网页内容。里面有些不常用的字段,为了最后导出效果好看,我没全部都导。
#有额外需要的可以自己改编,譬如分类啊,好评率啊等等。对我来说,知道价格、书名就够了
readdata<-function(i){
web<-read_html(url[i],encoding="UTF-8")
title<-web %>% html_nodes("div.zg_title") %>% html_text()
title_short<-substr(title,1,20)
price<-as.numeric(gsub("¥ ","",web %>% html_nodes("div.zg_itemPriceBlock_normal strong.price") %>% html_text()))
ranking_movement<-web %>% html_nodes("span.zg_salesMovement") %>% html_text()
rank_number<-as.numeric(gsub("\\.","",web %>% html_nodes("span.zg_rankNumber") %>% html_text()))
#新书榜里没有销售变动记录,所以记为NA
if (length(ranking_movement)==0) {ranking_movement=rep(NA,20)
rank_number=rep(NA,20)}
link<-gsub("\\\n","",web %>% html_nodes("div.zg_title a") %>% html_attr("href"))
ASIN<-sapply(strsplit(link,split = "/dp/"),function(e)e[2])
img<-web %>% html_nodes("div.zg_itemImage_normal img") %>% html_attr("src")
#这里加上html代码
img_link<-paste0("<img src='",img,"'>")
title_link<-paste0("<a href='",link,"'>",title_short,"</a>")
#合并数据
combine<-data.table(img_link,title_link,price,ranking_movement)
setnames(combine,c("图像","书名","价格","销售变动"))
Sys.sleep(5)
combine
}
transfer_html_table<-function(rawdata){
title<-paste0("<th>",names(rawdata),"</th>")
content<-sapply(rawdata,function(e)paste0("<td>",e,"</td>"))
content<-apply(content,1,function(e) paste0(e,collapse = ""))
content<-paste0("<tr>",content,"</tr>")
bbb<-c("<table border=1><tr>",title,"</tr>",content,"</table>")
bbb
}