【问题标题】:How to read the nth line of a Parsed html in R如何在 R 中读取 Parsed html 的第 n 行
【发布时间】:2014-10-10 09:54:58
【问题描述】:

readLines 函数将源页面的所有内容显示在一行中。

con = url("target_url_here")
htmlcode = readLines(con)

readLines 函数将源页面的所有行连接在一行中。所以我无法导航到原始 html 源页面的第 15 行。

下一个方法是尝试使用 XML 包或 httr 包来解析它。

library("httr")
html <- GET("target_url_here")
content2 = content(html,as="text")
parsedHtml = htmlParse(content2,asText=TRUE)

通过打印出已解析的Html,它保留了html格式并显示了在源页面中可以看到的所有内容。 现在假设我要提取标题,所以函数

xpathSApply(parsedHtml,"//title",xmlValue)

将给出标题。

但我的问题是,如何导航到 html 的第 15 行?换句话说,如何将 html 视为字符串向量,其中向量的每个元素都是 html 页面/已解析的 html 对象中的单独行。

【问题讨论】:

  • 嗯,通常readLines 会逐行读取,因此htmlcode[15] 应该在第一个示例中为您提供第 14 行。
  • 是的,这会奏效。但是有什么办法可以在解析的 HTML 对象中转到第 15 行?
  • @NovneetNov 也许将其转换为字符并在strsplit(as(parsedHtml, "character"), "\n")[[1]][15] 的脉络中分割换行。

标签: html r xml-parsing html-parsing


【解决方案1】:

仔细查看the docs for readLines(),它实际上返回了:

长度为读取行数的字符向量。

所以在你的情况下:

con = url("http://example.com/file_to_parse.html")
htmlCode = readLines(con)

您可以通过htmlCode[15] 轻松访问原始 html 源页面中的第 15 行。

【讨论】:

    【解决方案2】:

    回应您的评论

    但是有什么方法可以转到已解析的 HTML 对象的第 15 行吗?

    有几种不同的方法可以做到这一点。其中一个是 lukeA 在 cmets 中提到的。另一种是使用capture.output()逐行获取解析后的html文档作为字符向量。本示例使用来自?htmlParse的示例数据

    library(XML)
    f <- system.file("exampleData", "9003.html", package = "XML")
    

    解析一个html文档:

    ( doc <- htmlParse(f) )
    # <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    #     <html xmlns="http://www.w3.org/1999/xhtml">
    #     <head>
    #     <meta name="generator" content="HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
    #     <title>BKA/RIS VwGH - Volltext</title>
    #     <base target="_self">
    #     </head>
    #     <body>
    #     Veröffentlichungsdatum
    # </body>
    #     </html>
    

    将解析后的文档作为字符向量查看:

    capture.output(doc)
    # [1] "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">"
    # [2] "<html xmlns=\"http://www.w3.org/1999/xhtml\">"                                                                 
    # [3] "<head>"                                                                                                        
    # [4] "<meta name=\"generator\" content=\"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org\">"         
    # [5] "<title>BKA/RIS VwGH - Volltext</title>"                                                                        
    # [6] "<base target=\"_self\">"                                                                                       
    # [7] "</head>"                                                                                                       
    # [8] "<body>"                                                                                                        
    # [9] "Veröffentlichungsdatum"                                                                                       
    # [10] "</body>"                                                                                                       
    # [11] "</html>"                                                                                                       
    # [12] " "                                                                                                        
    

    获取(例如)第 5 行:

    capture.output(doc)[5]
    #[1] "<title>BKA/RIS VwGH - Volltext</title>"
    

    【讨论】:

      【解决方案3】:

      我发现这很好用,它不光滑,但它可以完成工作。

      library(XML)
      url <-"your desired website"
      html <-htmlTreeParse(url, useInternalNodes = T)
      text <-readLines(url)
      text
      nchar (text[15])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-20
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 1970-01-01
        • 1970-01-01
        • 2013-01-29
        相关资源
        最近更新 更多