Jan 20 2010

New version of Mini Bots PHP Class (v.1.4)

Category: Php, Spiders & webbotsadmin @ 11:53 pm

I’ve added three more bots to the Mini Bots Php Class, now the version number is 1.4 and it has these new features.

Follow the link, watch demos and download the class from the Mini Bots Php Class page:

  1. addeded twitterInfo method to retrieve the numbers of following, followers, and lists.
  2. addeded url2pdf method to convert and save locally a page to a pdf (thanks to pdfmyurl.com service).
  3. addeded webpage2txt method to extract all the text from a url, stripping html but also scripts and styles.

What else do you need? Suggest some usefull small bots. Comment to suggest.

  • Share/Bookmark

Tags: , , , , , ,


Jan 16 2010

PHP Web page to text function

Category: Php, Spiders & webbotsadmin @ 2:55 pm

I’ve found this nice small bot on the www.php.net site, thanks to the author of the script on the preg_replace page.
This bot returns the text content of a url and it could be used to take text from a site and find relevant words to search.

It’s nice because it uses CURL and let us see some nice stuff that CURL does:

  1. hide himself presenting with a specific user agent, to seem a browser and not a spider
  2. follows redirect (this means that you can call this function with a tiny url to retrieve the text in the real page!)
  3. use very powerful regular expression to remove html tags, but also javascript and styles (they remain if you simply do a strip_tags)

This script will be included in the next version of the Mini Bots PHP Class.

function webpage2txt($url) {
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

	$ch = curl_init();    // initialize curl handle
	curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
	curl_setopt($ch, CURLOPT_PORT, 80);            //Set the port number
	curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s

	curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

	$document = curl_exec($ch);

	$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
		'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
		'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
		'@<![\s\S]*?–[ \t\n\r]*>@',         // Strip multi-line comments including CDATA
		'/\s{2,}/',
	);

	$text = preg_replace($search, "\n", html_entity_decode($document));

	$pat[0] = "/^\s+/";
	$pat[2] = "/\s+\$/";
	$rep[0] = "";
	$rep[2] = " ";

	$text = preg_replace($pat, $rep, trim($text));

	return $text;
}

echo webpage2txt("http://www.rockit.it");
  • Share/Bookmark

Tags: , , , ,


Dec 24 2009

PHP bot to grab meteo information from Google

Category: Php, Spiders & webbotsadmin @ 3:37 pm

Google has many usefull functions that give you data fast, such as cinema infos, or for meteo forecasts. I think that Google grabs those informations from the many sites indexed with his bots.

As I did on a previous post for words spelling you can retrieve those informations with some mini bots. The mini bot I’ve made for meteo retrieves informations from italian Google servers about weather forecast of a specified city (not only italian cities).

Since google gives only 4 days meteo if you ask for a date too much in the future it will return an empty string.

Below you can find the PHP source code, and here is a working demo!

This function is included in the Mini Bots Class.

function dayadd($days,$date=null , $format="d/m/Y"){
	//add days to a date function
	return date($format,strtotime($days." days",strtotime( $date ? $date : date($format) )));
}

function attr($s,$attrname) {
	//get the attribute value of an html tag
	preg_match_all('#\s*('.$attrname.')\s*=\s*["|\']([^"\']*)["|\']\s*#i', $s, $x);
	if (count($x)>=3) return $x[2][0];
	return "";
}

function doGoogleMeteo($q,$date) {
	if ($date>dayadd(3,date("Y-m-d"),"Y-m-d"))return "";

	// grab google page with meteo query
	$web_page = file_get_contents( "http://www.google.it/search?q=meteo+" . urlencode($q) );

	//parse html to find data, and store them in an array
	preg_match_all('#<div class=e>(.*)</table>#Us', $web_page, $m);
	if (count($m)>0) {
		$p = array();
		preg_match_all('#<img([^>]*)?>#Us', $m[0][0], $img);
		for ($i=0;$i<count($img[0]);$i++) {
			$tag = str_replace("src=\"/","src=\"http://www.google.it/",$img[0][$i]);
			$p[dayadd($i,date("Y-m-d"),"Y-m-d")]["title"] = attr($tag,"title");
			$p[dayadd($i,date("Y-m-d"),"Y-m-d")]["img"] = attr($tag,"src");
		}
		preg_match_all('#<nobr>(.*)</nobr>#Uis', $m[0][0], $nobr);
		for ($i=0;$i<count($nobr[1]);$i++) {
			$temp= explode("|",$nobr[1][$i]);
			$p[dayadd($i,date("Y-m-d"),"Y-m-d")]["min"] = trim($temp[1]);
			$p[dayadd($i,date("Y-m-d"),"Y-m-d")]["max"] = trim($temp[0]);
		}
		return $p[$date];
	}

	return "nada.";
}

print_r ( doGoogleMeteo("milano","2009-12-25") );
//Array (
// [title] => Rovesci
// [img] => http://www.google.it/images/weather/rain.gif
// [min] => -4°C
// [max] => 7°C
//)
  • Share/Bookmark

Tags: , , , , , , , , ,


Next Page »