2016-03-29 7 views
-1

Versuchen, einen Schaber für die erste Notwendigkeit, die Daten zwischen den <a> Tags kratzen machen. Ich habe den Code ein wenig modifiziert, um die Daten dazwischen zu extrahieren. Hier ist der Code.Php Schaber extrahieren Daten nur die zwischen Tags

<?php 

    function scrape_between($data, $start, $end){ 
    $data = stristr($data, $start); 
    $data = substr($data, strlen($start)); 
    $stop = strpos($data, $end); 
    $data = substr($data, 0, $stop); 
    return $data; 
} 

function cURL($url) { 
    $options = array(
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_FOLLOWLOCATION => TRUE, 
     CURLOPT_AUTOREFERER => TRUE, 
     CURLOPT_CONNECTTIMEOUT => 120, 
     CURLOPT_MAXREDIRS => 10, 
     CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1", 
     CURLOPT_URL => $url, 
    ); 
    $ch = curl_init(); 
    curl_setopt_array($ch, $options); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 
    $url = "http://www.imdb.com/search/title?genres=action"; 
    $results_page = curl($url); 
    $results_page = scrape_between($results_page, "<div id=\"main\">", "<div id=\"sidebar\">"); 
    $separate_results = explode("<td class=\"title\">", $results_page); 
    foreach ($separate_results as $separate_result) { 
     if ($separate_result != " ") { 
     $results_urls[] = "http://www.imdb.com " . scrape_between($separate_result, "<a href=", "a>"); 
     } 
    } 
    print_r($results_urls); 
?> 

Was ich suche ist, die Daten in Form einer Liste der Titel der Filme kommen zu machen. Was ist der richtige Weg, um dies zu erreichen, da ich nicht sicher bin, ob Regex hier verwendet wird oder nicht?

+1

Mögliches Duplikat von [Tags für reguläre Übereinstimmungen mit Ausnahme von eigenständigen XHTML-Tags] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – andrew

+0

Dann benutze einfach DOM! – Ikari

+1

Anstatt ihre Website zu scrappen, sollten Sie hier beginnen: http://www.imdb.com/licensing/ // Diese Frage könnte auch in dieser Hinsicht interessant sein, http://stackoverflow.com/questions/1966503/does -imdb-provide-an-api – CBroe

Antwort

1

Dies kann helfen.

Es holt den Film Meta Werte IMDB (ex. Bild, Titel und Umriss) in einen PHP-Array mit PHP DOMDocument und curl [mit separaten Funktionen für innere HTML Inhalte und attibute Werte jeden Tag Extrahieren (durch Abgleichen ID, Tag-Namen und Klasse).]:

<?php 

$dom = new DOMDocument; 

function disguise_curl($url) 
{ 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    $html= curl_exec($curl); 
    if($html=== false) 
    { 
     if($errno = curl_errno($curl)){ 
     $error_message = curl_strerror($errno); 
     $html= "cURL error ({$errno}): {$error_message}\n"; 
     } 
    } 
    curl_close($curl); 

    return $html; 
} 

function scrape_between($data, $start, $end){ 
     $data = stristr($data, $start); 
     $data = substr($data, strlen($start)); 
     $stop = stripos($data, $end); 
     $data = substr($data, 0, $stop); 
     return $data; 
    } 

function getHTMLByID($id, $html) { 
    $dom = new DOMDocument; 
    libxml_use_internal_errors(true); 
    $dom->validateOnParse = true; 
    $dom->loadHTML($html); 
    $node = $dom->getElementById($id); 
    if($node) { 
     return $dom->saveHTML($node); 
    } 
    return FALSE; 
} 

function getHTMLByClass($class, $html, $bring_tag=false){ 
    $dom = new DOMDocument; 
    libxml_use_internal_errors(true); 
    $dom->validateOnParse = true; 
    $dom->loadHTML($html); 
    $class_arr= array(); 
    $xpath= new DOMXPath($dom); 
    $results = $xpath->query("//*[contains(@class, '$class')]"); 
    if($results->length > 0){ 
     foreach($results as $tag) 
     { 
      if($bring_tag===true) 
       array_push($class_arr, $tag); 
      else 
       array_push($class_arr, $dom->saveHTML($tag)); 
     } 
    }  
    return $class_arr; 
} 

function get_domattr($html, $tag, $attr) 
{ 
    $attr_vals= array(); 
    if(!empty($html)) 
    { 
    $dom = new DOMDocument; 
    libxml_use_internal_errors(true); 
    $dom->validateOnParse = true; 
    $dom->loadHTML($html); 
    foreach($dom->getElementsByTagName($tag) as $img) 
    array_push($attr_vals, $img->getAttribute($attr)); 
    } 
    return $attr_vals; 
} 

function getHTMLByTag($tag, $html) { 
    $attr_vals= array(); 
    if(!empty($html)) 
    { 
    global $dom; 
    libxml_use_internal_errors(true); 
    $dom->validateOnParse = true; 
    $dom->loadHTML($html); 

    foreach($dom->getElementsByTagName($tag) as $taghtml) 
     array_push($attr_vals, $dom->saveXML($taghtml)); 
    } 
    return $attr_vals; 
} 

$url= "http://www.imdb.com/search/title?genres=action"; 
$page_html= disguise_curl($url); 


$result_html= getHTMLByClass('image', $page_html); 

$movie_list= array(); 
$i=0; 
foreach($result_html as $cont_tag) 
{ 
    $img_link= get_domattr($cont_tag, 'img', 'src'); 
    if((!isset($img_link)) || (empty($img_link))) 
     $movie_list[$i]['photo']= 'na'; 
    else 
     $movie_list[$i]['photo']= $img_link[0]; 

    ++$i; 
} 

$result_html= getHTMLByClass('title', $page_html); 
$link_pre= 'http://imdb.com'; 

$i=0; 
foreach($result_html as $cont_tag) 
{ 
    $mtitle= getHTMLByTag('a', $cont_tag); 
    if((!isset($mtitle)) || (empty($mtitle))) 
     $movie_list[$i]['title']= 'na'; 
    else 
     $movie_list[$i]['title']= $mtitle[0]; 

    $mlink= get_domattr($cont_tag, 'a', 'href'); 
    if((!isset($mlink)) || (empty($mlink))) 
     $movie_list[$i]['link']= 'na'; 
    else 
     $movie_list[$i]['link']= $link_pre.''.$mlink[0]; 


    $moutline= getHTMLByClass('outline', $cont_tag); 
    if((!isset($moutline)) || (empty($moutline))) 
     $movie_list[$i]['outline']= 'na'; 
    else 
     $movie_list[$i]['outline']= $moutline[0]; 




    ++$i; 
} 

echo '<pre>'; 
print_r($movie_list); 
echo '</pre>'; 
?> 

Beispielausgabe:

Array 
(
    [0] => Array 
     (
      [photo] => http://ia.media-imdb.com/images/M/[email protected]_V1._SX54_CR0,0,54,74_.jpg 
      [title] => Captain America: Civil War 
      [link] => http://imdb.com/title/tt3498820/ 
      [outline] => Political interference in the Avengers' activities causes a rift between former allies Captain America and Iron Man. 
     ) 

    [1] => Array 
     (
      [photo] => http://ia.media-imdb.com/images/M/[email protected]_V1._SX54_CR0,0,54,74_.jpg 
      [title] => Batman v Superman: Dawn of Justice 
      [link] => http://imdb.com/title/tt2975590/ 
      [outline] => Fearing the actions of Superman are left unchecked, Batman takes on the man of steel, while the world wrestles with what kind of a hero it really needs. With Batman and Superman fighting each other, a new threat, Doomsday, is created by Lex Luthor. It's up to Superman and Batman to set aside their differences along with Wonder Woman to stop Lex Luthor and Doomsday from destroying Metropolis. 
     ) 

    [2] => Array 
     (
      [photo] => http://ia.media-imdb.com/images/M/[email protected]_V1._SX54_CR0,0,54,74_.jpg 
      [title] => na 
      [link] => na 
      [outline] => na 
     ) 
) 
+0

Vielen Dank das wird mir helfen, Dinge für ein wenig zu sortieren –

+0

Sie sind willkommen – sariDon

Verwandte Themen