2014-11-04 1 views
-2

내가이 크롤러가이 크롤러을 개선하지만, 인덱스에만 도메인 루트 전합니다 mydomain.com하지만 /somethingelse.php하지, 내가 웹 사이트 내부 링크를 의미 /otherpage.html ...필요한 도움

을 이 스크립트를 수정하여 루트가 아닌 더 많은 페이지를 색인 할 수있는 방법이 있습니까?

<?php 
require_once('./pathtoconfig'); 
require_once('./functions.php') 
set_time_limit(500); 
error_reporting(-1);  
header('Content-Type: text/plain; charset=utf-8;'); 

$db = @mysqli_connect($conf['host'], $conf['user'], $conf['pass'], $conf['name']); 
mysqli_query($db, 'SET NAMES utf8'); 

if(!$db) { 
    echo "Failed to connect to MySQL: (" . mysqli_connect_errno() . ") " . mysqli_connect_error(); 
} 

//Insert links separated by commas. 

$url = array('mydomain1.com', 'mydomain2.com');       
foreach($url as $k) {  
    $url = parse_url($k); 
    if(!isset($url['path'])) { 
     $selectData = "SELECT * FROM web WHERE url = '$k'"; 
     if(mysqli_fetch_row(mysqli_query($db, $selectData)) == null) { 
      $content = getUrl($k); 
      preg_match('#<title>(.*)</title>#i', $content, $title); 
      preg_match_all('/<img src=.([^"\' ]+)/', $content, $img); 
      preg_match('/<head>.+<meta name="description" content=.([^"\']+)/is', $content, $description); 
      preg_match('/<head>.+<meta name="author" content=.([^"\']+)/is', $content, $author); 
      #preg_match_all('/href=.([^"\' ]+)/i', $content, $anchor); 
      preg_match('/<body.*?>(.*?)<\/body>/is', $content, $body); 
      if(!empty($title[1]) AND !empty($description[1]) || !empty($body[1])) { 
       echo 'Title: '; @print_r($title[1]); 
       echo "\n"; 
       $body_trim = trim(preg_replace("/&#?[a-z0-9]+;/i",'',(strip_tags(@$body[0])))); $bodyContent = substr(preg_replace('/\s+/', ' ', $body_trim), 0, 255); 

       $description_trim = trim(preg_replace("/&#?[a-z0-9]+;/i",'',(strip_tags(@$description[1])))); $descContent = substr(preg_replace('/\s+/', ' ',$description_trim), 0, 255); 

       $bodyContent = str_replace('\'', '', $bodyContent); 
       $descContent = str_replace('\'', '', $descContent); 
       echo 'Description: '; @print_r($descContent); 
       echo "\n"; 
       echo 'Author: '; @print_r($author[1]); 
       echo "\n"; 
       echo 'URL: '; @print_r($k); $date = date("d M Y"); 
       echo "\n"; 
       echo "\n---------------------------------------------------------------------------\n"; 
       $insertData = "INSERT INTO `web` (`url` , `title` , `description` , `body` , `author`, `date`) VALUES ('".$k."', '"[email protected]$title[1]."', '"[email protected]$descContent."', '"[email protected]$bodyContent."', '"[email protected]$author[1]."', '".$date."')"; 
       #echo $insertData; 
       mysqli_query($db, $insertData); 
      } 
     } 
    } 
} 
?> 

희망을 주셔서 감사합니다. 정말 감사합니다.

+0

로드 한 문서 내에서 링크를 찾으려고 했습니까? –

+0

예. .com 뒤에 확장자가 있으면로드되지 않습니다. –

+1

문서 내에서 링크를 찾는 코드는 어디에 있습니까? –

답변

0

명시 적으로 아무도 없다는 것을 확인로, 경로 정보와 URL을 허용하지 않습니다 크롤러 : 당신은 (}을 닫는 일치와 함께) 모두이 테스트를 제거 할 수

if(!isset($url['path'])) { 

가, 또는 테스트를 요구 사항에 더 적합하게 변경하십시오.

+0

정말 고마워요. 링크를 수동으로 추가해도 작동하지만, 도메인 자체의 링크를 깊이 검색 할 수있는 방법이 있습니까? –

+0

페이지에서 링크를 찾으려면 @ rjdown의 답을 살펴보십시오. 원본 시드 목록 외에도 그 결과에 대한 작업 만하면됩니다. 오프 사이트 링크를 필터링 할 수도 있습니다. 그렇지 않으면 끝낼 수 있습니다. 당신이 원하는 것보다 훨씬 더 많은 다운로드. –

0

정규식을 사용하여 HTML을 구문 분석하지 마십시오. 대신 DomDocument를 사용하십시오. 그러면 모든 링크를 쉽게 찾을 수 있습니다. 여기에 빠른 Google에서 찾은 기능이 있습니다 ...이 기능이 얼마나 단순한 지 확인할 수 있습니다!

/** 
* @author Jay Gilford 
*/ 

/** 
* get_links() 
* 
* @param string $url 
* @return array 
*/ 
function get_links($url) { 

    // Create a new DOM Document to hold our webpage structure 
    $xml = new DOMDocument(); 

    // Load the url's contents into the DOM (the @ supresses any errors from invalid XML) 
    @$xml->loadHTMLFile($url); 

    // Empty array to hold all links to return 
    $links = array(); 

    //Loop through each <a> and </a> tag in the dom and add it to the link array 
    foreach($xml->getElementsByTagName('a') as $link) { 
     $links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue); 
    } 

    //Return the links 
    return $links; 
} 
+0

고마워요,하지만 문제는 크롤러 내에서 코드를 다시 작성하는 데 필요한 충분한 지식이 없습니다. –