【问题标题】:no applicable method for 'xpathApply' applied to an object of class "NULL"没有适用于“xpathApply”的方法应用于“NULL”类的对象
【发布时间】:2014-04-01 18:48:07
【问题描述】:

我有以下代码我不知道为什么会收到此错误:

rm(list=ls())
require("XML")
# <a href="/music/The+Beatles/Sgt.+Pepper%27s+Lonely+Hearts+Club+Band" 
beatles = "http://www.last.fm/music/The+Beatles/"

beatles.albums.page = paste(sep="", beatles, "+albums")
lines = readLines(beatles.albums.page)
album.lines = grep(pattern="href.*link-reference", lines, value=TRUE)
album.names = sub(pattern=".*<h3>(.*)</h3>.*", replacement="\\1", x=album.lines)
album.names = gsub(pattern=" ", replacement="+", x=album.names)
album.names = gsub(pattern="'", replacement="%27", x=album.names)

for (album in album.names[1]) {
  print(album)
  album.link = paste(sep="", beatles, album)
  print(album.link)
  tables = readHTMLTable(album.link)

}

有什么想法吗?

【问题讨论】:

  • readHTMLTable(album.link) 行导致错误。
  • 它有什么问题?怎么解决?
  • last.fm 确实有一个 API - last.fm/api - 这将使这个比刮擦干净得多
  • 他们的 API 有 R 脚本吗?

标签: html regex r web-scraping last.fm


【解决方案1】:

线

readHTMLTable(album.link)

导致错误。尝试将其更改为

tables = readHTMLTable(album.link, header = FALSE)

但它仍然给你警告:

Warning message:
In readLines(beatles.albums.page) :
  incomplete final line found on 'http://www.last.fm/music/The+Beatles/+albums'

你可以摆脱它

readLines(beatles.albums.page, warn = FALSE) 

另请注意,您并没有“保存”表格,它在每个循环中都会更改,但也许这就是您想要的。

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 2022-12-15
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    相关资源
    最近更新 更多