Skip to content

Commit

Permalink
Update scraper.php
Browse files Browse the repository at this point in the history
  • Loading branch information
mycwnet committed Sep 19, 2018
1 parent adddae9 commit 04a1423
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions scraper.php
@@ -1,27 +1,33 @@
<?
// This is a template for a PHP scraper on morph.io (https://morph.io)
// including some code snippets below that you should find helpful

// require 'scraperwiki.php';
// require 'scraperwiki/simple_html_dom.php';
//
// // Read in a page
// $html = scraperwiki::scrape("http://foo.com");
//
// // Find something on the page using css selectors
// $dom = new simple_html_dom();
// $dom->load($html);
// print_r($dom->find("table.list"));
//
// // Write out to the sqlite database using scraperwiki library
// scraperwiki::save_sqlite(array('name'), array('name' => 'susan', 'occupation' => 'software developer'));
//
// // An arbitrary query against the database
// scraperwiki::select("* from data where 'name'='peter'")

// You don't have to do things with the ScraperWiki library.
// You can use whatever libraries you want: https://morph.io/documentation/php
// All that matters is that your final data is written to an SQLite database
// called "data.sqlite" in the current working directory which has at least a table
// called "data".
<?php
require 'scraperwiki.php';
######################################
# Basic PHP scraper
# Credit to Martin Draexler this code is forked from their scrapper
# https://github.com/draexler/imdb-top-250
# I couldn't use most of that data an wanted the ID (which that scraper lacked)
# and Rank Only
######################################
$html = scraperwiki::scrape("http://www.imdb.com/chart/top");
$html = oneline($html);
preg_match_all('|<tr bgcolor="#.*?" valign="top"><td align="right"><font face="Arial, Helvetica, sans-serif" size="-1"><b>(.*?)\.</b></font></td><td align="center"><font face="Arial, Helvetica, sans-serif" size="-1">.*?</font></td><td><font face="Arial, Helvetica, sans-serif" size="-1"><a href="(.*?)">.*?</a> \.*?\)</font></td><td align="right"><font face="Arial, Helvetica, sans-serif" size="-1">.*?</font></td></tr>|',$html,$arr);

foreach ($arr[1] as $key=>$val) {
scraperwiki::save(array('rank'), array('rank' => "".clean($arr[1][$key]),'imdb_id' => clean($arr[2][$key])));
}
function clean($val) {
$val = str_replace('&nbsp;',' ',$val);
$val = str_replace('&amp;','&',$val);
$val = html_entity_decode($val);
$val = strip_tags($val);
$val = trim($val);
$val = utf8_decode($val);
return($val);
}

function oneline($code) {
$code = str_replace("\n",'',$code);
$code = str_replace("\r",'',$code);
return $code;
}
?>

0 comments on commit 04a1423

Please sign in to comment.