2014-01-31 4 views
0

나는이 나는 이름, 도시, departament처럼,이 URL에서 정보를 받고 있어요 및 URL의 목록이 스크립트 등PHP - 웹 스크래핑 - cURL을 사용하여 캐시하는 방법?

이 제 기능 중 일부입니다 :

function getCity($url) 
    { 
    $url = curl_get_contents($url); 
    $html_object = str_get_html($url); 
    return $ret = $html_object->find('td', 86)->plaintext; 
    } 

function getDepartment($url) 
    { 
    $url = curl_get_contents($url); 
    $html_object = str_get_html($url); 
    return $ret = $html_object->find('td', 90)->plaintext; 
    } 

function getSalary($url) 
    { 
    $url = curl_get_contents($url); 
    $html_object = str_get_html($url); 
    $ret = $html_object->find('td', 94)->plaintext; 
    return trim($ret); 
    } 

이 내 암호 코드 :

function curl_get_contents($url) 
{ 
    $curl_moteur = curl_init(); 
    curl_setopt($curl_moteur, CURLOPT_URL, $url); 
    curl_setopt($curl_moteur, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($curl_moteur,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 

    curl_setopt($curl_moteur, CURLOPT_FOLLOWLOCATION, 1); 
    $web = curl_exec($curl_moteur); 
    curl_close($curl_moteur); 
    return $web; 
} 

자세히 알 수 있듯이 각 필드에 대해 요청하는 중입니다. 매우 비효율적입니다. 각 URL을 요청하는 모든 정보 필드를 한 번만 추출하려면 캐시를 구현하고 싶습니다.

미리 감사드립니다.

+0

그냥 파일에 $ 웹 수익을 저장하고 필요한 시간을 엽니 다

은 또는 사용하지 않으려면 것은 쉬운 수정 static 변수를 사용하는 것입니다 객체. – Kei

답변

0

당신은 너무처럼 기능에서 클래스를 만들 수 있습니다

class Scrapper 
{ 
    public $page_content; 

    public $html_object; 

    public function __construct($url) 
    { 
     $this->page_content = $this->curl_get_contents($url); //in case you want to keep for something scrapped url content 
     $this->html_object = $this->str_get_html($this->page_content); //create object from html, probably simpleXML 
    } 

    public function getCity() 
    { 
     return $this->html_object->find('td', 86)->plaintext; 
    } 

    public function getDepartment() 
    { 
     return $this->html_object->find('td', 90)->plaintext; 
    } 

    public function getSalary() 
    { 

     $ret = $this->html_object->find('td', 94)->plaintext; 
     return trim($ret); 
    } 

    public function curl_get_contents($url) 
    { 
     $curl_moteur = curl_init(); 
     curl_setopt($curl_moteur, CURLOPT_URL, $url); 
     curl_setopt($curl_moteur, CURLOPT_RETURNTRANSFER, 1); 

     curl_setopt($curl_moteur,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 

     curl_setopt($curl_moteur, CURLOPT_FOLLOWLOCATION, 1); 
     $web = curl_exec($curl_moteur); 
     curl_close($curl_moteur); 
     return $web; 
    } 

    public function str_get_html() 
    { 
     //unkown function content 
     $this->html_object = $some_object; // $some_object = str_get_html($url) from your code; 
    } 
} 

$scrapper = new Scrapper($your_url); 

echo $scrapper->getCity(); 
echo $scrapper->getDepartment(); 

주 그 코드가 테스트되지 않은 것입니다.

이렇게하면 클래스를 인스턴스화 할 때 URL을 한 번 요청할 수 있습니다.

function curl_get_contents($url) 
{ 
    static $web = null; 
    if (!is_null($web)) { 
    return $web; 
    } 

    $curl_moteur = curl_init(); 
    curl_setopt($curl_moteur, CURLOPT_URL, $url); 
    curl_setopt($curl_moteur, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($curl_moteur,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 

    curl_setopt($curl_moteur, CURLOPT_FOLLOWLOCATION, 1); 
    $web = curl_exec($curl_moteur); 
    curl_close($curl_moteur); 
    return $web; 
} 
관련 문제