i made code reads table website , writes on mine. want read specific rows/columns , write on site. table filled weather data , refreshes every 5 minutes. need values full , half hours , not values in row, temperature. example, there's row every 5 minutes containing temperature value, humidity, sun radiation etc. need find value of, let's 05:00, , read/write temperature column of row. in case be: 05:00 12,5°c. , need 48 values, because there's 24 hours per day , including 24 half hours it's 48 together, right..
this part of code:
<?php $trazi = ':00'; $citaj = file('proba.txt'); foreach($citaj $linija) { if(strpos($linija, $trazi) !== false) echo $linija; } $traziurl = "somepage"; $stranica = file_get_contents($traziurl); $tablica = '/(<table.*<\/table>)/s'; preg_match_all($tablica, $stranica, $zeit); echo $zeit[0][0]; $ime = "proba.txt"; $table = fopen($ime, 'w') or die ("error!"); $podaci = $zeit[0][0]; fwrite($table, $podaci); fclose($table); ?>
there's chance won't work 'cause parts missing, give idea.
i'm sure there multiple other ways this, i'd this.
<?php /** * @author bart degryse * @copyright 2013 */ function getdata() { //get html page $url = "http://www.essen-wetter.de/table.php"; $content = file_get_contents($url); //turn dom document searchable xpath $dom = new domdocument(); $dom->loadhtml($content); $xpath = new domxpath($dom); //get field names $query = "//tr/td[position()=1 , normalize-space(text()) = 'zeit']"; $entries = $xpath->query($query); $entry = $entries->item(0); $tr = $entry->parentnode; foreach ($tr->getelementsbytagname("td") $td) { $fieldnames[] = $td->textcontent; } //get field data $query = "//tr/td[position()=1 , (substring-after(normalize-space(text()),':') = '00' or substring-after(normalize-space(text()),':') = '30')]"; $entries = $xpath->query($query); foreach ($entries $entry) { $fieldvalues = array(); $tr = $entry->parentnode; foreach ($tr->getelementsbytagname("td") $td) { $fieldvalues[] = $td->textcontent; } $data[] = array_combine($fieldnames, $fieldvalues); } //return data set return $data; } //gather data $data = getdata(); //do echo "<pre>\n"; foreach ($data $row) { echo "temperature @ {$row['zeit']} {$row['temperatur']}.\n"; } echo "</pre><hr><pre>\n"; print_r($data); echo "</pre>\n"; ?>
if you're going display data on utf-8 compatible terminal or on web page that's declared being utf-8 encoded should it. if you're want use single-byte iso-8859-1 encoding you'll have change line:
$fieldnames[] = $td->textcontent;
into this:
$fieldvalues[] = utf8_decode($td->textcontent);
remark please note while doing technically not hard legally you're on loose ground. data on page copyrighted , owned markus wolter. using data own purposes without consent considered theft.
Comments
Post a Comment