javascript - 抓取javascript网站

标签 javascript xml r web-scraping screen-scraping

我能够从基本的 html 页面上抓取数据,但我在抓取下面的网站时遇到了问题。看起来数据是通过 javascript 呈现的,我不确定如何处理该问题。如果可能的话,我更愿意使用 R 进行抓取,但也可以使用 Python。

有什么想法/建议吗?

编辑:我需要获取每个列表的年份/制造商/型号、序列号、价格、位置和简短描述(以“拍卖:”开头)。

http://www.machinerytrader.com/list/list.aspx?bcatid=4&DidSearch=1&EID=1&LP=MAT&ETID=5&catid=1015&mdlx=Contains&Cond=All&SO=26&btnSearch=Search&units=imperial

最佳答案

library(XML) 
library(relenium)

##downloading website
website<- firefoxClass$new() 
website$get("http://www.machinerytrader.com/list/list.aspx?pg=1&bcatid=4&DidSearch=1&EID=1&LP=MAT&ETID=5&catid=1015&mdlx=Contains&Cond=All&SO=26&btnSearch=Search&units=imperial") 
doc <- htmlParse(website$getPageSource())

##reading tables and binding the information
tables <- readHTMLTable(doc, stringsAsFactors=FALSE)
data<-do.call("rbind", tables[seq(from=8, to=56, by=2)])
data<-cbind(data, sapply(lapply(tables[seq(from=9, to=57, by=2)],  '[[', i=2), '[', 1))
rownames(data)<-NULL
names(data) <- c("year.man.model", "s.n", "price", "location", "auction")

这将为您提供第一页所需的内容(此处仅显示前两行):

head(data,2)
      year.man.model      s.n      price location                                               auction
1 1972 AMERICAN 5530 GS14745W US $50,100       MI                   Auction: 1/9/2013; 4,796 Hours;  ..
2 AUSTIN-WESTERN 307      307  US $3,400       MT Auction: 12/18/2013;  AUSTIN-WESTERN track excavator.

要获取所有页面,只需遍历它们,在地址中粘贴 pg=i

关于javascript - 抓取javascript网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22204382/

相关文章:

r - Distinct (dplyr) 效果不佳 - 基于标准的独特观察

r - 在 R 中按名称和日期添加唯一 ID

javascript - 滚动到纯 JavaScript 不起作用

javascript - 在 AngularJS 指令中使用 `require`

javascript - 如何在 Angular Reactive Form 的嵌套数组字段中构建字段数组?

java - 哪个数组声明提高了软件的可维护性

Android格式化xml字符串丢失html标签

javascript - 将按钮文本获取到数组中

java - 使用正则表达式解析字符串后创建并返回节点列表

r - R 中按组对变量求和