Sunday, December 12, 2010

How To Build A Basic Web Crawler To Pull Information From A Website (Part 1)

How To Build A Basic Web Crawler To Pull Information From A Website (Part 1): "

web crawlerWeb Crawlers, sometimes called scrapers, automatically scan the Internet attempting to glean context and meaning of the content they find. The web wouldn’t function without them. Crawlers are the backbone of search engines which, combined with clever algorithms, work out the relevance of your page to a given keyword set.

The Google web crawler will enter your domain and scan every page of your website, extracting page titles, descriptions, keywords, and links – then report back to Google HQ and add the information to their huge database.

Today, I’d like to teach you how to make your own basic crawler – not one that scans the whole Internet, though, but one that is able to extract all the links from a given webpage.


Generally, you should make sure you have permission before scraping random websites, as most people consider it to be a very grey legal area. Still, as I say, the web wouldn’t function without these kind of crawlers, so it’s important you understand how they work and how easy they are to make.

To make a simple crawler, we’ll be using the most common programming language of the internet – PHP. Don’t worry if you’ve never programmed in PHP – I’ll be taking you through each step and explaining what each part does. I am going to assume an absolute basic knowledge of HTML though, enough that you understand how a link or image is added to an HTML document.

Before we start, you will need a server to run PHP. You have a number of options here:

Getting Started

We’ll be using a helper class called Simple HTML DOM. Download this zip file, unzip it, and upload the simple_html_dom.php file contained within to your website first (in the same directory you’ll be running your programs from). It contains functions we will be using to traverse the elements of a webpage more easily. That zip file also contains today’s example code.

First, let’s write a simple program that will check if PHP is working or not. We’ll also import the helper file we’ll be using later. Make a new file in your web directory, and call it example1.php – the actual name isn’t important, but the .php ending is. Copy and paste this code into it:

<?php
include_once('simple_html_dom.php');
phpinfo();
?>

Access the file through your internet browser. If you don’t have a server set up, you can still run the program from my server if you want. If everything has gone right, you should see a big page of random debug and server information printed out like below – all from the little line of code! It’s not really what we’re after, but at least we know everything is working.

web crawler

The first and last lines simply tell the server we are going to be using PHP code. This is important because we can actually include standard HTML on the page too, and it will render just fine. The second line pulls in the Simple HTML DOM helper we will be using. The phpinfo(); line is the one that printed out all that debug info, but you can go ahead and delete that now. Notice that in PHP, any commands we have must be finished with a colon (;). The most common mistake of any PHP beginner is to forget that little bit of punctuation.

One typical task that Google performs is to pull all the links from a page and see which sites they are endorsing. Try the following code next, in a new file if you like.

<?php
include_once('simple_html_dom.php');

$target_url = “http://www.tokyobit.com/”;
$html = new simple_html_dom();
$html->load_file($target_url);
foreach($html->find(‘a’) as $link){
echo $link->href.”<br />”;
}
?>

Again, you can run that from my server too if you don’t have your own set up. You should get a page full of URLs! Wonderful. Most of them will be internal links, of course. In a real world situation, Google would ignore internal links and simply look at what other websites you’re linking to, but that’s outside the scope of this tutorial.

If you’re running on your own server, go ahead and change the target_URL variable to your own webpage or any other website you’d like to examine.

That code was quite a jump from the last example, so let’s go through in pseudo-code to make sure you understand what’s going on.

Include once the simple HTML DOM helper file.

Set the target URL as http://www.tokyobit.com.

Create a new simple HTML DOM object to store the target page

Load our target URL into that object

For each link <a> that we find on the target page

- Print out the HREF attribute

That’s it for today, but if you’d like a bit of challenge – try to modify to the second example so that instead of searching for links (<a> elements), it grabs images instead (<img>). Remember, the src attribute of an image specifies the URL for that image, not HREF.

Would you like learn more? Let me know in the comments if you’re interested in reading a part 2 (complete with homework solution!), or even if you’d like a back-basics PHP tutorial – and I’ll rustle one up next time for you. I warn you though – once you get started with programming in PHP, you’ll start making plans to create the next Facebook, and all those latent desires for world domination will soon consume you. Programming is fun.


Got Questions? Ask Them Now FREE on MakeUseOf Answers!


Similar MakeUseOf Articles



"

Find Beautiful Free eBooks In Google’s New Shop [US Only]


google ebooksWith access to millions of books and compatibility with a wide variety of devices, Google’s new book store is certainly going to be a powerful force in the publishing industry. Even better, it provides easy access to beautifully rendered public domain books free of charge. Get yourself a quality collection of classic literature free from Google.
Google’s entry into the ebook store business will undoubtedly be controversial, but one thing is clear: their collection of public domain ebooks is without rival in terms of volume and quality. Best of all, with the help of Calibre, you can read these books on practically any device – even the seemingly unsupported Amazon Kindle.

Radio Scanner – Live Police Scanner App For The Android Phone

Radio Scanner – Live Police Scanner App For The Android Phone: "

live police scannerAre you always the last person to hear about the latest news? Many people buy police and emergency scanner radios just so that they can be the first ones to hear about an accident, a fire, a fight or any other event in their city or town. In certain areas of the world, the radio traffic on police and emergency scanners can be more entertaining than prime time television.

I’ve always wanted a scanner at home, but I never got around to actually buying one. So, you can imagine my excitement when I learned that the Android Market has a very popular live police scanner application offered for free called Scanner Radio by Gordon Edwards.


People use a lot of tools to find out what’s happening locally, whether it’s local news sites, local newspaper resources as Saikat described, or sites for discovering local events like Steven mentioned. But if you really want to be in the loop and on top of breaking local news events, Scanner Radio provides live streaming audio from police, emergency and weather base stations all around the world.

You can search by the most popular scanners, or locate the ones nearest you. The streaming audio for each area is provided by gracious volunteers that stream their scanner audio via computer to RadioReference.com, Wunderground.com or LiveFireFeeds.com. Any feeds uploaded to those sites will show up in the thousands of Scanner Radio listings.

Looking For Interesting Live Police Scanners

I have to warn you, this is one of the most addictive apps on the Android market. The cool thing about this app is that no matter where in the world you live, you can listen to local scanner radio traffic of any region where a kind volunteer is streaming the audio to the Internet. If you own a scanner in your part of the world, consider contributing to the cause!

Once you install the app, the main page lets you look for interesting scanners by region, “genre” (rail, marine, weather), or focused on one of the source websites listed above.

live police scanner

The other nice feature is that you can add an alert for any scanner you find. An alert is a setting where once the listener count reaches a certain limit, your phone will alert you. Usually when there’s a significant event going on, the listener count for that scanner spikes.

If you just want to listen, click on one of the scanners you find and the player will open up in its own window. Even if you close the app at this point, the player will still run in the background, so you’ll hear radio updates out of your phone speaker whenever they occur.

If you really want to listen to some major drama, more exciting than just about any reality show you’ll find on TV, check out the “Top 50 Scanners” option from the main menu. This list displays the scanners with the highest volume of listeners, which typically means the highest radio traffic as well.

When I clicked on the NYPD scanner, I was completely mesmerized for over an hour as police pulled over a drunk driver, broke up a serious gang fight, and investigated a domestic assault incident.

When I switched to the Chicago PD scanner, it was even more enthralling – gang fights, explosions and assorted criminals getting arrested. You can’t find this level of action anywhere else, and it’s all live!

If you click on the “Details” button at the bottom of a scanner, if the volunteer included any information on the scanner, you’ll find it there. For example, the NYPD details included the precincts covered by the scanner and a few of the most popular police 10 codes you’ll hear over the radio.

Download Radio Scanner with this code:

Supplement Your Scanner With Signals

Now that you’ve transformed your Android into a personal live police scanner, you’re going to start hearing a whole lot of conversations littered with strange codes. If you want to make it a lot easier to understand what’s going on, make sure to download Signals.

Signals is an insanely comprehensive list of symbols, signals and codes for things like marine, police, sign language, braille and even computer symbols! The police codes are pretty detailed – and the section that will help the most when you’re listening to your scanner is the Police Penal Codes page.

Beyond radio communications codes, Signals even helps you out with understanding electronic communications, like Text Messaging shorthand. If you think you know all of them, you won’t believe the crazy shorthand acronyms listed in this section.

Another entertaining reference is the Emoticons page – which features every emoticon under the sun.

So now, when you hear about an amazing breaking news event on your Android radio scanner, you can quickly fire off an appropriately brisk text message to a friend letting them know about the news, finished off with an intrigued emoticon.

You can download Signals here:

live police scanner

Are you a radio scanner listener? Do you listen to local radio traffic, or do you prefer live police scanners for other parts of the world? What resources do you use to listen to live police scanners on the web? Share your insight in the comments section below.


Do you like MakeUseOf articles? Don’t forget to share our articles with others! It’s really important to us.

Similar MakeUseOf Articles



"

Cool Websites and Tools [December 9th]

Cool Websites and Tools [December 9th]: "

Check out some of the latest MakeUseOf discoveries. All listed websites are FREE or come with a decent free account option. You can make use of them without spending a dime. If you want to have similar cool websites round-ups delivered to your email daily email subscribe here.

Add Your Website Here!

StockTwits – Twitter users and stock traders have one thing in common – they are glued to their respective screens all the time. Monitoring latest stock updates and finding the latest tweets are almost the same activities. However, what if you can get instant tweets on your stock? Enter StockTwits. Read more: StockTwits: Check Latest Stock Updates & Advice In Real Time

GoogleDemoSlam – Creating a demo is one of the most effective ways to tell your audience what your tool can do. However, if your demo is boring and mundane, it can have a negative effect. If you want to learn how to create awesome demos, check out some really cool ones at GoogleDemoSlam. It is a collection of demos for services and tools based on Google technologies. Read more: GoogleDemoSlam: View & Rate Cool Demos For Various Tools

Zazu – Android phone owners have a number of free applications that can make their life easier. For example many people like to stay in touch with the news and weather. Zazu is one Android app that can read the news and weather out to you whenever you want. The app’s main function is to gather information on the local weather and global news. Read more: Zazu: Wake Up To Weather Reports, News or RSS Feeds (Android)

TestiPhone – Considering the huge popularity of the iPhone around the world, and its amazing web browsing capabilities, it becomes important for webmasters to make sure that their site displays correctly on the iPhone’s interface. TestiPhone.com is a nice web based tool that acts as an iPhone simulator, showing how a site looks on an iPhone. Read more: TestiPhone.com: Check How A Site Looks On iPhone

MapThatPad – Accommodation hunting can be a crazy feat, especially if you are new to the city and do not want to waste time checking out a dead end. That is why we turn to Craigslist – but then we run into the problem of sorting our listings properly to make the right call. Enter MapThatPad, a cool app based on Craigslist that you use to collaborate on apartment listings. Read more: MapThatPad: Hunting For Accommodation In An Easy & Organized Way

These are just half of the websites that we discovered in the last couple of days. If you want us to send you daily round-ups of all cool websites we come across, leave your email here. Or follow us via RSS feed.


Hey Facebookers, make sure to check out MakeUseOf page on Facebook. Over 24,000 fans already!

Similar MakeUseOf Articles



"