How to get (scrap, crawl) value of unique element by ID from external website using php?

How to get (scrap) value of unique element by ID from external website using php?

 

1 way:

if (!function_exists('goaskle_get_data_from_url')) {

function goaskle_get_data_from_url($url) {
$ch = curl_init();
$timeout = 5;

$userAgent = 'Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0';
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}

$cache_file = 'solnechiny-temp-sea.cache';

if(file_exists($cache_file) && filesize($cache_file) > 0) {
  if(time() - filemtime($cache_file) > 3600) {
$cache = goaskle_get_data_from_url('https://world-weather.ru/pogoda/abkhazia/sukhumi/water/');      

     file_put_contents($cache_file, $cache);
  } else {
     // cache is still fresh
       $cache = file_get_contents('solnechiny-temp-sea.cache');
  }
} else {
  // no cache, create one
$cache = goaskle_get_data_from_url('https://world-weather.ru/pogoda/abkhazia/sukhumi/water/');        
  file_put_contents($cache_file, $cache);
}

$content = $cache;

$dom = new DomDocument();

@ $dom->loadHTML($content,LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$table = $dom->getElementById('weather-now-number'); //DOMElement

echo '<div id="temp_black_sea_footer">'.$table->nodeValue.'</div>';

2 way:

$url = 'https://seatemperature.ru/current/abkhazia/gagra-sea-temperature';

$content = file_get_contents($url);

$dom = new DomDocument();

@ $dom->loadHTML($content,LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$element = $dom->getElementById('temp1'); //DOMElement

print_r($element->nodeValue); 

or

//$dom->saveHTML($element));

Метаданные статьи

Идентификатор статьи:
51
Категория:
Дата добавления:
08.06.2021 01:17:20
Просмотры:
881
Рейтинг (Голоса):
(1)