Font or problem with Unicode on the scraper

I am trying to clear the information from the site.

On that website

127 East Zhongshan No 2 Rd; 中山东二路127号

But when I try to cancel it and repeat it, it will show

127 East Zhongshan No 2 Rd; 中山ä¸äºè·¯127å· 

I am also trying to use UTF-8

There is my php code

now please help me solve this problem.

function GrabPage($site){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, $site);
    ob_start();
    return curl_exec ($ch);
    ob_end_clean();
    curl_close ($ch);
}
$GrabData   = GrabPage($site);

$dom    = new DOMDocument();
@$dom->loadHTML($GrabData);

$xpath  = new DOMXpath($dom);


$mainElements = array();
$mainElements = $xpath->query("//div[@class='col--one-whole mv--col--one-half wv--col--one-whole'][1]/dl/dt");

foreach ($mainElements as $Names2) {
    $Name2  = $Names2->nodeValue;
    echo "$Name2";
}
+4
source share
2 answers

First, you need to set the encoding before anything on top of the PHP file:

header('Content-Type: text/html; charset=utf-8');

You need to convert the html markup you got with mb_convert_encoding:

@$dom->loadHTML(mb_convert_encoding($GrabData, 'HTML-ENTITIES', 'UTF-8'));

Output example

+1
source

First of all, you need to check whether the captured HTML source is encoded correctly. If yes, try

utf8_decode($Name2)

,

0

Source: https://habr.com/ru/post/1584162/


All Articles