如何使用 php 从网络上抓取印地文文本

how to scrape hindi text from web using php

我在这里尝试从印地语的网络(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);
}

你们很亲近。您收到标志:ि 和 स

首先你可以尝试 google 这个字符,你会发现这些字符的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。下一个问题是,你想如何处理你的结果。

如果您是 运行 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);
}

我认为 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>