【问题标题】:how to scrape hindi text from web using php如何使用 php 从网络上抓取印地语文本
【发布时间】:2015-10-16 04:09:40
【问题描述】:

我正在尝试从印地语的网络(在 url 中)抓取数据,但我得到了这样的响应

\u093f\u0938\

如何解码这个 unicode?请建议我在 PHP 中执行我的脚本。

这个脚本在英文文本下正常工作,所以英文发生了什么。我已经用这个脚本抓取了数据。我知道这个响应是 dev nagri unicode 但如何解码它。

我是 php 问题的新手,在此先感谢

$i= 1;
for($i; $i < 6; $i++)
{
    $html file_get_contents("http://www.jagran.com/jokes/child/jokes-1262211".$i.".html");
    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    libxml_clear_errors();
    $nodes = $dom->getElementsByTagName('p');
    $item = array();
    $articles = array();
    foreach ($nodes as $node) {
         $item['msg'] = (strlen($node->nodeValue) > 20 ? $node->nodeValue : '');
         $item['cat_id'] = 1;
         if($item['msg'] !="")
         $articles[] = array_unique($item);
    }
    $articles = json_encode($articles);
    print_r($articles);
}

【问题讨论】:

  • 放在标题
  • 那个响应是 not unicode,这正是你的问题。它被转义了,因为传输层或其他一些元素使用不同的编码。会不会是你自己的系统没有加载配置phps多字节扩展?
  • @arkascha 这是印地语单词的devnagri代码

标签: php unicode page-curl


【解决方案1】:

你很亲密。你会收到符号:ि 和 स

首先你可以尝试用谷歌搜索字符,你会发现字符的devnagari含义:

https://www.google.de/#q=%5Cu093f

https://www.google.de/#q=%5Cu0938

如果您想在 html 中显示 unicode,您必须将编码从 /u0123 更改为 ģ。见这里:

<html>
<body>
<p>These are two chars in devnagari &#x93f;&#x938;<p>
</body>
</html>

但是,当您想要抓取印地语时,您应该开始学习如何阅读和处理 unicode。下一个问题是,你想如何处理你的结果。

【讨论】:

    【解决方案2】:

    如果您运行的是 PHP 5.4 或更高版本,请在调用 json_encode 时传递 JSON_UNESCAPED_UNICODE 参数。

    $i= 1;
    for($i; $i < 6; $i++)
    {
        $html file_get_contents("http://www.jagran.com/jokes/child/jokes-1262211".$i.".html");
        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        libxml_clear_errors();
        $nodes = $dom->getElementsByTagName('p');
        $item = array();
        $articles = array();
        foreach ($nodes as $node) {
             $item['msg'] = (strlen($node->nodeValue) > 20 ? $node->nodeValue : '');
             $item['cat_id'] = 1;
             if($item['msg'] !="")
             $articles[] = array_unique($item);
        }
        $articles = json_encode($articles, JSON_UNESCAPED_UNICODE);
    //--------------------add-this---------------------^
        print_r($articles);
    }
    

    【讨论】:

      【解决方案3】:

      我认为 PHPhil 的回答很好,我投了赞成票。我编辑了代码,因为它不能仅用于执行 php 部分——相反,添加正确的元标记(参见下面的代码)以正确显示 devnagari 很重要。我还想纠正缺少“=”的错误。不幸的是,我的编辑被拒绝了,所以我必须添加一个带有代码更正的新答案。

      <html>
      <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      </head>
      <body>
      <?php
      
      $i= 1;
      for($i; $i < 6; $i++)
      {
          $html = file_get_contents("http://www.jagran.com/jokes/child/jokes-1262211".$i.".html");
          libxml_use_internal_errors(true);
          $dom = new DOMDocument();
          $dom->loadHTML($html);
          libxml_clear_errors();
          $nodes = $dom->getElementsByTagName('p');
          $item = array();
          $articles = array();
          foreach ($nodes as $node) {
               $item['msg'] = (strlen($node->nodeValue) > 20 ? $node->nodeValue : '');
               $item['cat_id'] = 1;
               if($item['msg'] !="")
               $articles[] = array_unique($item);
          }
          $articles = json_encode($articles, JSON_UNESCAPED_UNICODE);
      //--------------------add-this---------------------^
          print_r($articles);
      }
      ?>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2017-06-21
        • 2018-03-26
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多