【问题标题】:web scraping in imdb using R使用 R 在 imdb 中进行网页抓取
【发布时间】:2014-05-11 21:37:39
【问题描述】:

我想在 imdb 中找到前 250 部电影的链接。我决定通过查看 HTML 源代码来找到一个通用模式。我找到了“chttp”,但我不确定它是否能把我带到任何地方。我怎样才能找到一种模式来在其上构建链接?

require("XML")
imdb="http://www.imdb.com/chart/top?sort=ir,desc"
imdb.page=readLines(imdb)
g = grep(pattern = "chttp", x = imdb_page) 
imdb.lines=imdb.page[g]

这是一个示例输出:

> imdb.lines[1]
[1] "      <h3><a href=\"/chart/?ref_=chttp_cht\" >IMDb Charts</a></h3>"

我的主要问题是试图根据我已经编写的代码找出 250 部热门电影中每一部的链接(URL)。我基本上不知道下一步是什么。此外,我不确定我对“chttp”使用 grep 命令的模式是否良好。

所以根据从索引 3 开始的结果,电影标题在奇数索引上:

> imdb.lines[1]
[1] "      <h3><a href=\"/chart/?ref_=chttp_cht\" >IMDb Charts</a></h3>"
> imdb.lines[2]
[1] "  <td class=\"posterColumn\"><a href=\"/title/tt0111161/?ref_=chttp_tt_1\" ><img src=\"http://ia.media-imdb.com/images/M/MV5BODU4MjU4NjIwNl5BMl5BanBnXkFtZTgwMDU2MjEyMDE@._V1_SX34_CR0,0,34,50_.jpg\" width=\"34\" height=\"50\" />"
> imdb.lines[3]
[1] "    <a href=\"/title/tt0111161/?ref_=chttp_tt_1\" title=\"Frank Darabont (dir.), Tim Robbins, Morgan Freeman\" >The Shawshank Redemption</a>"
> imdb.lines[6]
[1] "  <td class=\"posterColumn\"><a href=\"/title/tt0071562/?ref_=chttp_tt_3\" ><img src=\"http://ia.media-imdb.com/images/M/MV5BNDc2NTM3MzU1Nl5BMl5BanBnXkFtZTcwMTA5Mzg3OA@@._V1_SX34_CR0,0,34,50_.jpg\" width=\"34\" height=\"50\" />"
> imdb.lines[4]
[1] "  <td class=\"posterColumn\"><a href=\"/title/tt0068646/?ref_=chttp_tt_2\" ><img src=\"http://ia.media-imdb.com/images/M/MV5BMjEyMjcyNDI4MF5BMl5BanBnXkFtZTcwMDA5Mzg3OA@@._V1_SX34_CR0,0,34,50_.jpg\" width=\"34\" height=\"50\" />"
> imdb.lines[5]
[1] "    <a href=\"/title/tt0068646/?ref_=chttp_tt_2\" title=\"Francis Ford Coppola (dir.), Marlon Brando, Al Pacino\" >The Godfather</a>"
> imdb.lines[7]
[1] "    <a href=\"/title/tt0071562/?ref_=chttp_tt_3\" title=\"Francis Ford Coppola (dir.), Al Pacino, Robert De Niro\" >The Godfather: Part II</a>"
> imdb.lines[9]
[1] "    <a href=\"/title/tt0468569/?ref_=chttp_tt_4\" title=\"Christopher Nolan (dir.), Christian Bale, Heath Ledger\" >The Dark Knight</a>"
> imdb.lines[10]
[1] "  <td class=\"posterColumn\"><a href=\"/title/tt0110912/?ref_=chttp_tt_5\" ><img src=\"http://ia.media-imdb.com/images/M/MV5BMjE0ODk2NjczOV5BMl5BanBnXkFtZTYwNDQ0NDg4._V1_SY50_CR0,0,34,50_.jpg\" width=\"34\" height=\"50\" />"

【问题讨论】:

  • 我不知道为什么这个话题被搁置了!!!
  • xpath 非常简单。对于标题,请尝试:library(XML); tt &lt;- htmlParse('http://www.imdb.com/chart/top?sort=ir,desc'); xpathSApply(tt, "//td[@class='titleColumn']//a", xmlValue)。另请查看xpathSApply(tt, "//td[@class='titleColumn']//a", xmlAttrs)
  • 我喜欢这个,但我需要 URL 而不是电影名称。谢谢。
  • 是的,抱歉 - 我快速阅读了您的问题。我编辑了上面的评论。第二个代码块结果的第一行给出了 url。即xpathSApply(tt, "//td[@class='titleColumn']//a", xmlAttrs)[1, ].
  • 查看我之前的评论。只需将矩阵子集到第一行。或cbind 转置(即t())属性矩阵的标题。

标签: html r web-scraping imdb


【解决方案1】:

xpath 让这样的工作变得微不足道。

library(XML)
tt <- htmlParse('http://www.imdb.com/chart/top?sort=ir,desc')
cbind(xpathSApply(tt, "//td[@class='titleColumn']//a", xmlValue),
           t(xpathSApply(tt, "//td[@class='titleColumn']//a", xmlAttrs)))

cbind 的第一个参数返回标题(a 标签之间的文本),第二个参数返回锚的属性(href 和标题,在这种情况下,后者包含有关电影导演的详细信息) .

【讨论】:

  • 只是一个简单的问题。如何从 URL 中提取 tt0468569 之类的内容?
  • 有几种方法,但sub 非常适合:sub('.*/title/(.*)/.*', '\\1', s),其中s 是您想要执行此操作的字符串向量,例如我的解决方案输出的 url 列。有关正则表达式的更多详细信息,请参阅 hereherestringr 包为这类事情提供了一些便利功能。
【解决方案2】:

使用alternative interfaces 怎么样?

编辑#1:我查看了一些文件,似乎没有任何链接,甚至没有 imdb ID,但应该有另一种方法。

编辑#2:好的,显然没有其他方法,但有人已经做了一些事情。例如。 this guy;看看吧。

【讨论】:

  • 谢谢。我喜欢这个答案!
猜你喜欢
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
相关资源
最近更新 更多