【问题标题】:Scraping javascript website抓取 javascript 网站
【发布时间】:2014-04-07 21:39:16
【问题描述】:

我能够从基本的 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

【问题讨论】:

  • 查看 Selenium。在 SO 上有几个通过 R 使用它的例子,但并不多。
  • 使用CasperJS,它可以让你连接到页面,并等待元素被加载。您还可以将 JavaScript 直接注入到页面上下文中。

标签: javascript xml r web-scraping screen-scraping


【解决方案1】:
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

【讨论】:

  • 感谢您的快速回复。但是,当我运行此代码时,我得到空结果。 readHTMLTable 命令似乎并没有实际读取任何内容。它只是产生一个空列表。有什么想法吗?
  • 另外 - 我使用的是 Windows 7,如果这有区别的话。
  • 感谢您指出这一点,您是对的,我确实使用了允许直接下载的不同设置。我更新了答案,先用relenium下载源码,然后用readHTMLTable,现在应该可以用了!
【解决方案2】:

使用Relenium

require(relenium) # More info: https://github.com/LluisRamon/relenium
require(XML)
firefox <- firefoxClass$new() # init browser
res <- NULL
pages <- 1:2
for (page in pages) {
  url <- sprintf("http://www.machinerytrader.com/list/list.aspx?pg=%d&bcatid=4&DidSearch=1&EID=1&LP=MAT&ETID=5&catid=1015&mdlx=Contains&Cond=All&SO=26&btnSearch=Search&units=imperial", page)
  firefox$get(url) 
  doc <- htmlParse(firefox$getPageSource())
  res <- rbind(res, 
               cbind(year_manu_model = xpathSApply(doc, '//table[substring(@id, string-length(@id)-15) = "tblListingHeader"]/tbody/tr/td[1]', xmlValue),
                     sn = xpathSApply(doc, '//table[substring(@id, string-length(@id)-15) = "tblListingHeader"]/tbody/tr/td[2]', xmlValue),
                     price = xpathSApply(doc, '//table[substring(@id, string-length(@id)-15) = "tblListingHeader"]/tbody/tr/td[3]', xmlValue),
                     loc = xpathSApply(doc, '//table[substring(@id, string-length(@id)-15) = "tblListingHeader"]/tbody/tr/td[4]', xmlValue),
                     auc = xpathSApply(doc, '//table[substring(@id, string-length(@id)-9) = "tblContent"]/tbody/tr/td[2]', xmlValue))
  )
}
sapply(as.data.frame(res), substr, 0, 30)                        
#      year_manu_model                  sn               price         loc   auc                               
# [1,] " 1972 AMERICAN 5530"            "GS14745W"       "US $50,100"  "MI " "\n\t\t\t\t\tAuction: 1/9/2013; 4,796" 
# [2,] " AUSTIN-WESTERN 307"            "307"            "US $3,400"   "MT " "\n\t\t\t\t\tDetails & Photo(s)Video(" 
# ...

【讨论】:

  • 已安装 relenium,但当我在上面运行您的确切代码时出现“错误:WebDriverException”。关于可能导致此问题的任何想法?
  • @lukeA - 错误消失了,但“auc”字段有两个问题:1)它没有拉出全文,2)它交替拉出一些“详细信息和照片”文本原因(例如:第 1 条记录拉取拍卖数据,第 2 条记录拉取 Details & Photos,第 3 条记录拉取拍卖数据……)。有什么想法吗?
  • 找出第一个问题 - 只需将 sapply 参数从 30 设置为 300。还看到“auc”字段出于某种原因正在拉入 \n\t\t\t\t\。
  • @user3384596 sapply 将输出缩短一点,存储在res 中。您应该能够使用例如轻松去除尾随控制字符stringr::str_trim()tm::stripWhitespace() 或只是 gsub。对于另一个问题:调整 xpath 以满足您的需求。
猜你喜欢
  • 2011-03-22
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多