10

Important note: the questions below aren't meant to break ANY data copyrights. All crawled and saved data is being linked directly to the source.


For a client I'm gathering information for building a search engine/web spider combination. I do have experience with indexing webpages' inner links with a specific depth. I also have experience in scraping data from webpages. However, in this case, the volume is larger than I have experience with so I was hoping to gain some knowledge and insights in the best practice to do so.

First of all, what I need to make clear is that the client is going to deliver a list of websites that are going to be indexed. So, in fact, a vertical search engine. The results only need to have a link, title and description (like the way Google displays results). The main purpose of this search engine is to make it easier for visitors to search large amounts of sites and results to find what they need. So: Website A containts a bunch of links -> save all links together with meta data.

Secondly, there's a more specific search engine. One that also indexes all the links to (let's call them) articles, these articles are spread over many smaller sites with a smaller amount of articles compared to the sites that end up in the vertical search engine. The reason is simple: the articles found on these pages have to be scraped in as many details as possible. This is where the first problem lies: it would take a huge amount of time to write a scraper for each website, data that needs to be collected is for example: city name, article date, article title. So: Website B contains more detailed articles than website A, we are going to index these articles and scrape usefull data.

I do have a method in my mind which might work, but that involves writing a scraper for each individual website, in fact it's the only solution I can think of right now. Since the DOM of each page is completely different I see no option to build a fool-proof algorithm that searches the DOM and 'knows' what part of the page is a location (however... it's a possibility if you can match the text against a full list of cities).

A few things that crossed my mind:

Vertical Search Engine

  • For the vertical search engine it's pretty straight forward, we have a list of webpages that need to be indexed, it should be fairly simple to crawl all pages that match a regular expression and store the full list of these URLs in a database.
  • I might want to split up saving page data (meta description, title, etc) into a seperate process to speed up the indexing.
  • There is a possbility that there will be duplicate data in this search engine due to websites that have matching results/articles. I haven't made my mind up on how to filter these duplicates, perhaps on article title but in the business segment where the data comes from there's a huge change on duplicate titles but different articles

Page scraping

  • Indexing the 'to-be-scraped'-pages can be done in a similar way, as long as we know what regex to match the URLs with. We can save the list of URLs in a database
  • Use a seperate process that runs all individual pages, based on the URL, the scraper should now what regex to use to match the needed details on the page and write these to the database
  • There are enough sites that index results already, so my guess is there should be a way to create a scraping algorithm that knows how to read the pages without having to match the regex completely. As I said before: if I have a full list of city names, there must be an option to use a search algorithm to get the city name without having to say the city name lies in "#content .about .city".

Data redundance

An important part of the spider/crawler is to prevent it from indexing duplicate data. What I was hoping to do is to keep track of the time a crawler starts indexing a website and when it ends, then I'd also keep track of the 'last update time' of an article (based on the URL to the article) and remove all articles that are older than the starting time of the crawl. Because as far as I can see, these articles do no longer exists.

The data reduncance is easier with the page scraper, since my client made a list of "good sources" (read: pages with unique articles). Data redundance for the vertical search engine is harder, because the sites that are being indexed already make their own selection of artciles from "good sources". So there's a chance that multiple sites have a selection from the same sources.


How to make the results searchable

This is a question apart from how to crawl and scrape pages, because once all data is stored in the database, it needs to be searchable in high speed. The amounts of data that are going to be saved is still unknown, compared to some competition my client had an indication of about 10,000 smaller records (vertical search) and maybe 4,000 larger records with more detailed information.

I understand that this is still a small amount compared to some databases you've possibly been working on. But in the end there might be up to 10-20 search fields that a user can use the find what they are looking for. With a high traffic volume and a lot of these searches I can imagine that using regular MySQL queries for search isn't a clever idea.

So far I've found SphinxSearch and ElasticSearch. I haven't worked with any of them and haven't really looked into the possibilities of both, only thing I know is that both should perform well with high volume and larger search queries within data.


To sum things up

To sum all things up, here's a shortlist of questions I have:

  • Is there an easy way to create a search algorithm that is able to match DOM data without having to specify the exact div the content lies within?
  • What is the best practice for crawling pages (links, title & description)
  • Should I split crawling URLs and saving page title/description for speed?
  • Are there out-of-the-box solutions for PHP to find (possible) duplicate data in a database (even if there are minor differences, like: if 80% matches -> mark as duplicate)
  • What is the best way to create a future proof search engine for the data (keep in mind that the amounts of data can increase aswel as the site traffic and search requests)

I hope I made all things clear and I'm sorry for the huge amount of text. I guess it does show that I spend some time already in trying to figure things out myself.

2
  • Matching location by text alone is probably out of the question unless you have certain guarantees about your data in all cases. For example, imagine an article on a Portland Oregon website about a man from Jacksonville Florida. Which do you match? The first occurrence? Imagine if you have the same article but the article's publish location is blank. Now the only city mentioned is Jacksonville Florida but it isn't the article location.
    – Mr. Llama
    Nov 4, 2014 at 15:36
  • Jep, that's what aching my head aswel, however in the type of articles we are going to index/scrape there's a smaller chance for this to happen. But the data needs to be as relevant as possible anyway. Nov 4, 2014 at 16:06

7 Answers 7

10
+500

I have experience building large scale web scrapers and can testify that there will always be big challenges to overcome when undertaking this task. Web scrapers run into problems ranging from CPU issues to storage to network problems and any custom scraper needs to be built modular enough to prevent changes in one part from breaking the application as a whole. In my projects I have taken the following approach:

Figure out where your application can be logically split up

For me this meant building 3 distinct sections:

  1. Web Scraper Manager

  2. Web Scraper

  3. HTML Processor

The work could then be divided up like so:

1) The Web Scraper Manager

The Web Scraper Manager pulls URL's to be scraped and spawns Web Scrapers. The Web Scraper Manager needs to flag all URL's that have been sent to the web scrapers as being "actively scraped" and know not to pull them down again while they are in that state. Upon receiving a message from the scrapers the manager will either delete the row or leave it in the "actively scraped" state if no errors occurred, otherwise it will reset it back to "inactive"

2) The Web Scraper

The web Scraper receives a URL to scrape and goes about CURLing it and downloading the HTML. All of this HTML can then be stored in a relational database with the following structure

ID | URL | HTML (BLOB) | PROCESSING

Processing is an integer flag which indicates whether or not the data is currently being processed. This lets other parsers know not to pull the data if it is already being looked at.

3) The HTML Processor

The HTML Processor will continually read from the HTML table, marking rows as active every time it pulls a new entry. The HTML processor has the freedom to operate on the HTML for as long as needed to parse out any data. This can be links to other pages in the site which could be placed back in the URL table to start the process again, any relevant data (meta tags, etc.), images etc.

Once all relevant data has been parsed out the HTML processor would send all this data into an ElasticSearch cluster. ElasticSearch provides lightning-fast full text searches which could be made even faster by splitting the data into various keys:

{ 
   "url" : "http://example.com",
   "meta" : {
       "title" : "The meta title from the page",
       "description" : "The meta description from the page",
       "keywords" : "the,keywords,for,this,page"
   },
   "body" : "The body content in it's entirety",
   "images" : [
       "image1.png",
       "image2.png"
   ]
}

Now your website/service can have access to the latest data in real time. The parser would need to be verbose enough to handle any errors so it can set the processing flag to false if it cannot pull data out, or at least log it somewhere so it can be reviewed.

What are the advantages?

The advantage of this approach is that at any time if you want to change the way you are pulling data, processing data or storing data you can change just that piece without having to re-architect the entire application. Further, if one part of the scraper/application breaks the rest can continue to run without any data loss and without stopping other processes

What are the disadvantages?

It's a big complex system. Any time you have a big complex system you are asking for big complex bugs. Unfortunately web scraping and data processing are complex undertaking and in my experience there is no way around having a complex solution to this particularly complex problem.

7
  • This is one of the answers I was looking for, thanks! I split up 1 & 2/3 in my mind. But splitting it up one extra time is perhaps even better. Do you have any info's on making the data searchable? ElasticSearch looks the best so far to do this, as far as I've seen it's easy to integrate in the process. Nov 7, 2014 at 12:14
  • Here is a good reference on searching large data sets within Elasticsearch: exploringelasticsearch.com/searching_a_book.html I have used ES with much success in a couple projects and found it to be fairly easy to set up and extremely powerful when integrated with Nov 7, 2014 at 16:27
  • 2
    That's a good idea, I will do that Nov 9, 2014 at 17:02
  • 1
    @Joshua-Pendo Turns out Kumar was involved in some voting fraud. 7 of those votes were fake. Be careful out there. Feb 18, 2015 at 21:59
  • 1
    Thanks for the heads up, but in the end this was kind of the best way to go anyway. I'm currently working on the final version for my client and it works as expected. We're just waiting to check the speed of the scraper while running multiple instances at a time. Feb 20, 2015 at 12:39
3

The crawling and indexing actions can take a while, but you won't be crawling the same site every 2 minutes, so you can consider an algorithm in which you put more effort in crawling and indexing your data, and another algorithm to help you get a faster search.

You can keep crawling your data all the time and update the rest of the tables in the background (every X minutes/hours), so your search results will be fresh all the time but you won't have to wait for the crawl to end.

Crawling

Just get all the data you can (probably all the HTML code) and store it in a simple table. You'll need this data for the indexing analysis. This table might be big but you don't need good performance while working with it because it's going to be part of a background use and it's not going to be exposed for user's searches.

ALL_DATA
____________________________________________
| Url | Title | Description | HTML_Content |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

Tables and Indexing

Create a big table that contains URLs and keywords

KEYWORDS
_________________
| URL | Keyword |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

This table will contain most of the words in each URL content (I would remove words like "the", "on", "with", "a" etc...

Create a table with keywords. For each occurrence add 1 to the occurrences column

KEYWORDS
_______________________________
| URL | Keyword | Occurrences |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

Create another table with "hot" keywords which will be much smaller

HOT_KEYWORDS
_________________
| URL | Keyword | 
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

This table content will be loaded later according to search queries. The most common search words will be store in the HOT_KEYWORDS table.

Another table will hold cached search results

CACHED_RESULTS
_________________
| Keyword | Url |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

Searching algorithm

First, you'll search the cached result table. In case you have enough results, select them. If you don't, search the bigger KEYWORDS table. Your data is not that big so searching according to the keyword index won't take too long. If you find more relevant results add them to the cache for later usage.

Note: You have to select an algorithm in order to keep your CACHED_RESULTS table small (maybe to save the last use of the record and remove the oldest record if the cache is full).

This way the cache table will help you reduce the load on the keywords tables and give you ultra fast results for the common searches.

1
  • Thanks, however you do rely on a MySQL table to index the searches. As far as I've dived into ElasticSearch, that is a way better (and future) proof option than using a MySQL database to index search results. In essence, you're doing the same as I'm about to do (besides the search method). Also I'm thinking of not storing the full page source but do a direct DOM-inspection for URLs I need and store those. Nov 4, 2014 at 11:53
3
+150
  • Just look at the Solr and solr-wiki. its a open source search platform from the lucene project(similar like Elasticsearch).
  • For web crawler, you can use either Aperture or Nutch. Both are written in java. Aperture is a light weight crawler. But with Nutch we can crawl 1000 even more websites.
  • Nutch will handle the process of crawling for websites. Moreover Nutch provides Solr support. It means that you can index the data crawled from Nutch directly into Solr.
  • Using Solr Cloud we can setup multiple clusters with shards and replication to prevent the data loss and fast data retrieving.

Implementing your own web crawler is not that much easy and for search, regular RDBMS is much complicated to retrieve the data at run time.

3
  • Looking at the plusses, I guess it's a good answer. But in order for me to award you the 500 points I'd expect a little more detailed information about the how-to's and why you made these choices. Like, I'm a PHP/MySQL developer, why should I choose Java driven scraper? Performance? and how easy would it be for me to get used to both Nutch and Solr? Thanks so far for you efforts anyhow and I'm looking forward to reading a bigger answer from your side. Nov 7, 2014 at 12:16
  • Due to the amount of upvotes I asked Jonathan Crowe to reward you 150 rep anyway. Thanks for your answer Nov 11, 2014 at 7:27
  • Hope my answer could help you in someway. Thanks.
    – Kumar
    Nov 11, 2014 at 7:33
2

I've had my experiences with crawling websites and is a really complicated topic. Whenever I've got some problem withing this area, I look what the best people at this do (yup, google). They have a lot of nice presentations about what they are doing and they even release some (of their) tools. phpQuery for example is a great tool when it comes to searching specific data on a website, I'd recommend to have a look at it if you don't know it yet.

A little trick I've done in a similar project was to have two tables for the data. The data had to be as up to date as possible, so the crawler was running most of the time and there were problems with locked tables. So whenever the crawler wrote into one table, the other one was free to the search engine and vice versa.

1
  • I didn't come across phpQuery yet, thanks. Also good thinking about the two-table setup, certainly future proof. Nov 4, 2014 at 16:09
1

I have built a Web Crawler for detecting news sites - and its performing very well. It basically downloads the the whole page and then saves it prepares that for another scraping which is looking for keywords. It then basicallly tries to determine if the site is relevant using keywords. Dead simple.

You can find the sourcecode for it here. Please help contribute :-) It's a focused crawler which doesnt really do anything else than look for sites and rank them according to the presence of keywords. Its not usable for huge data loads, but it's a quite good at finding relevant sites.

https://github.com/herreovertidogrom/crawler.git

It's a bit poorly documented - but I will get around to that.

If you want to do searches of the crawled data, and you have a lot of data, and aspire to build a future proof service - you should NOT create a table with N columns, one for each search term. This is a common design if you think the URL is the primary key. Rather, you should avoid a wide-table design like the pest. This is because IO disk reads get incredibly slow on wide table designs. You should instead store all data in one table, specify the key and the value, and then partition the table on variable name.

Avoiding duplicates is always hard. In my experience, from data warehousing - design the primary key and let the DB do the job. I try to use the source + key + value as a primary key makes you avoid double counting, and has few restrictions.

May I suggest you create a table like this :

URL, variable, value and make that primary key.

Then write all data into that table, partition on distinct variable and implement search on this table only. It avoids duplicates, it's fast and easily compressable.

1
  • Thanks, this is already a little more in the right direction. As far as I can see this is what I had in mind until now. Hopefully I'll get some more detailed/wider answers. Nov 3, 2014 at 9:01
-1

Did you tried the http://simplehtmldom.sourceforge.net/manual.htm? I found it useful for scrapping the pages and it might be helpful the search the contents.

use an asynchronous approach to crawl and store the data, so that you can run multiple parallel crawling and storing

ElasticSearch will be useful to search the stored data.

1
  • See the comment above: I'm aware of how to scrape and index pages. But I'm more curious to find ways to build a scraper that knows/learns how to scrape data without having to identify it to a single div that holds the desired content. Nov 2, 2014 at 15:09
-2

You can search the HTML using this code:

<? 
    //Get the HTML
    $page = file_get_html('http://www.google.com')

    //Parse the HTML
    $html = new DOMDocument();
    $html->loadHTML($page);

    //Get the elemnts you are intersted in... 
    $divArr = $html->getElementsByTagName('div');
    foreach($divArr as $div) {
        echo $div->nodeValue;
    }
?>
2
  • I know how to search the page content, also how to use the regex to get what I want. But I'm looking for a global answer combining everything in combination with a searchable database (high performance) Oct 31, 2014 at 16:33
  • @SammyJankis, you should learn about the Symfony's DomCrawler component, definitely more efficient than a simple DOMDocument parser.
    – Alain
    Nov 4, 2014 at 13:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.