Pintrest Pin Scraper

bosshog

Developer
Feb 5, 2009
12
0
0
TX
www.formlogger.com
Scrape pins from pintrest using a keyword list. Results will be saved to file.
Simply edit the keyword filename, and output filename and run this file.

Code:
<?php

/****************************
* pinterest pin scraper
* 6/28/
*****************************/

// output filename

$output_filename = 'pin_urls.txt';
$keyword_filename = 'keywords.txt';


function curly($url) {

    $ch=curl_init();
    CURL_SETOPT($ch,CURLOPT_URL,$url);
    CURL_SETOPT($ch,CURLOPT_FOLLOWLOCATION,1);
    CURL_SETOPT($ch,CURLOPT_RETURNTRANSFER,1);
    $data = curl_exec($ch); 
    return $data;

}

function scrape_pins($kw) {
	  
	$data = curly('http://pinterest.com/search/pins/?q='.str_replace(" ","+",$kw));	
	file_put_contents("data.html",$data);	
	preg_match_all("/href=\"\/pin\/([0-9]+)\/\"\sclass=\"pinImageWrapper/",$data,$matches);
	$pins = array();	

	foreach($matches[1] as $pin_id) {	
	    $pins[] = 'http://pinterest.com/pin/'.$pin_id;
	}
	
	return $pins;
}


// open text file of keywords
$keywords = file($keyword_filename);

// loop through keywords and scape pins for each
$pin_urls = array();

foreach($keywords as $kw) {
	$pin_urls = scrape_pins($kw);
	
	foreach($pin_urls as $pin_url) {
	    print $pin_url."\r\n";	 
	    file_put_contents($output_filename,$pin_url."\r\n",FILE_APPEND);	    
	}
}








?>