drisate Posted December 31, 2014 Share Posted December 31, 2014 Hey guys i need help with a CURL code. I need to retreive all the data in a table located at http://www.listenlive.nl/tvgenre.php?g=1&page=0 and parse the data to finish with an array that looks like this:$row[0][station_name] = '3BTV';$row[0][stream_link] = '420';$row[0][stream_type] = 'wma';$row[0][country] = 'United Kingdom';$row[0][genre][0] = 'Variety';$row[0][genre][1] = 'Alternative';$row[0][genre][2] = 'Culture';$row[0][rating] = '1/5';I need that for each rows of the table where the first array number would increment $row[1], $row[2] ...My PHP code is incomplete. I currently have a loop for all the TR's of the page ... and when i loop all the TD's inside the TR's it returns a load of crap ... I am unsure where to go from here. Any help would be apreciated thxThis is my code: $ch = curl_init(); $categ[url] = 'http://www.listenlive.nl/tvgenre.php?g=1&page=0'; curl_setopt($ch, CURLOPT_URL, $categ[url]); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); curl_setopt($ch, CURLOPT_COOKIE, $cookies); curl_setopt($ch, CURLOPT_REFERER, $categ[url]); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_USERAGENT, $nav); $data2 = curl_exec($ch); $dom2 = new DOMDocument(); libxml_use_internal_errors(true); $dom2->loadHTML($data2); $trs = $dom2->getElementsByTagName('tr'); foreach ($trs as $tr) { echo "<pre>"; print_r($tr); echo "</pre>"; } Link to comment https://forums.phpfreaks.com/topic/293568-table-to-array/ Share on other sites More sharing options...
Zane Posted December 31, 2014 Share Posted December 31, 2014 what is the output of your print_r statement? That would help Link to comment https://forums.phpfreaks.com/topic/293568-table-to-array/#findComment-1501355 Share on other sites More sharing options...
drisate Posted December 31, 2014 Author Share Posted December 31, 2014 Well as you can imagine, the dump of the $tr var is very big ... would be a lot easyer if you would just execute the provided code. Link to comment https://forums.phpfreaks.com/topic/293568-table-to-array/#findComment-1501357 Share on other sites More sharing options...
Zane Posted December 31, 2014 Share Posted December 31, 2014 On 12/31/2014 at 7:17 PM, drisate said: Well as you can imagine, the dump of the $tr var is very big So trim it down for us. Show us the relevant information. Link to comment https://forums.phpfreaks.com/topic/293568-table-to-array/#findComment-1501358 Share on other sites More sharing options...
drisate Posted December 31, 2014 Author Share Posted December 31, 2014 Ok well i am gething closser ... I decided to use the xpath querry to point at the correct table in the page. It will be alot easyer to pin-point the data this way. Quote $ch = curl_init(); $categ[url] = 'http://www.listenlive.nl/tvgenre.php?g=1&page=0'; curl_setopt($ch, CURLOPT_URL, $categ); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); curl_setopt($ch, CURLOPT_COOKIE, $cookies); curl_setopt($ch, CURLOPT_REFERER, $categ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_USERAGENT, $nav); $data2 = curl_exec($ch); $dom2 = new DOMDocument(); libxml_use_internal_errors(true); $dom2->loadHTML($data2); $xpath = new DOMXpath($dom2); $elements = $xpath->query("/html/body/table[3]"); foreach ($elements as $element) { echo "<pre>"; print_r($element); echo "</pre>"; } The dump looks like: http://eric.webiummedia.com/scrap.phpNow what i need is from the $elements loop all the TD's and retrive the HTML inside Link to comment https://forums.phpfreaks.com/topic/293568-table-to-array/#findComment-1501369 Share on other sites More sharing options...
Frank_b Posted January 1, 2015 Share Posted January 1, 2015 foreach($element as $row) { echo $row['name']; echo $row['url']; } Link to comment https://forums.phpfreaks.com/topic/293568-table-to-array/#findComment-1501445 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.