【问题标题】:uiwebview javascript HTML get user facing body text onlyuiwebview javascript HTML 仅获取面向用户的正文文本
【发布时间】:2012-06-16 01:54:35
【问题描述】:

我在 UIWebView 中加载了以下 html 源代码
我要提取
text1
文本 2 文本 2
文本3 文本3 文本3

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>1322170516271</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=1, minimum-scale=1.0, maximum-scale=4.0">                   

    <style type="text/css">
    body
    {
        padding: 5px;
        margin: 0px;
        font-family: Helvetica, Arial;
        font-size: 12pt;
        background-color: #efefef;
        background-image: url(ArticleBackground.jpg);
        background-position: cover;
        color: #000000;
    }
    h1
    {
        text-align: center;
        border-bottom: 1px dotted #805050;
        font-size: 28px;
        line-height: 38px;
        margin-bottom: 30px;
        text-shadow: 0 2px 1px white;
        color: #803030;
    }
    </style>

</head>

<body>

    <script type="text/javascript">
    function printMe()
    {
        print();
    }
    </script>

    <div style='align:center; padding: 20px;'>

        <div>

    <b>text1</b><br><br>

    <h2>
      text2 text2
    </h2>
    <br>
    text3 text3 text3

        </div>

    </div>

</body>
</html>

但这是我使用时得到的结果

[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"]

我不需要 body 和 h1。我只想要用户面对的实际文本。

234534546



    body
{
    padding: 5px;
    margin: 0px;
    font-family: Helvetica, Arial;
    font-size: 12pt;
    background-color: #efefef;
    background-image: url(ArticleBackground.jpg);
    background-position: cover;
    color: #000000;
}
h1
{
    text-align: center;
    border-bottom: 1px dotted #805050;
    font-size: 28px;
    line-height: 38px;
    margin-bottom: 30px;
    text-shadow: 0 2px 1px white;
    color: #803030;
}







    function printMe()
    {
        print();
    }






text1


  text2 text2


text3 text3 text3

感谢您的任何见解。

更新

[webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"] 对我的目标也不起作用

<script type="text/javascript">
    function printMe()
    {
        print();
    }
    </script>

    <div style="align:center; padding: 20px;">

        <div>

    <b>text1</b><br><br>

    <h2>
       text2 text2
    </h2>
    <br>
    text3 text3 text3

        </div>

    </div>

更新:这是现有项目所必需的。如果我有机会重新设计它,将很容易找到解决方案。但考虑到这个 HTML 源代码,它可能会有点困难。

【问题讨论】:

  • 根据更新有什么想法吗?

标签: javascript iphone ios uiwebview document-body


【解决方案1】:

尝试使用:

document.body.innerHTML

或者看看解析HTML:parsing HTML on the iPhone SO上有很多其他的链接。

【讨论】:

  • 是的,我试过那个 samjeev,但这也不好。将更新我的描述。如果可能的话,我不想为此添加解析器。使用简单的 javascript 调用将其加载到 Web 视图中后,这似乎是可行的。
【解决方案2】:

为什么不将所有文本放入不同的标签中,例如 div、p 等。给它们每个id,然后通过语法获取它们中的文本

var text1 = document.getElementById("your ID").innerHTML

希望这能解决您的问题。

【讨论】:

  • 是的,我希望我能做到,但为时已晚。我们正在谈论大量已经存在的 html 文件,这将是一个昂贵的选择。所以我没有办法以这种方式解决它。不过感谢您的建议。
猜你喜欢
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多