22

I am trying to do a very simple web crawler/spider app in C++. I have been searching using Google for a simple one to understand the concept. I found this:

spider_simpleCrawler

However, it is complicated to understand for me, since I started learning C++ about 1 month ago.

This is, for example, what I'm trying to do:

  1. Enter the URL: www.example.com (I will use bash->wget, to get the contents/source code),

  2. Look for, maybe "a href" link, and then store in some data file.

Is there a simpler tutorial or guide on the Internet?

Yun
  • 3,056
  • 6
  • 9
  • 28
popurity09
  • 255
  • 1
  • 3
  • 7
  • 3
    First learn C++ properly, then attempt to do something as complicated as a crawler. – Fred Foo Nov 25 '10 at 14:32
  • 3
    Hmm... Even a simple crawler? Not as complicated as larbin. I think, while doing the crawler will help me learn lots of things in c++. Guess, my thinking is wrong – popurity09 Nov 25 '10 at 14:36
  • consider using https://github.com/Microsoft/cpprestsdk – Sergei Krivonos Apr 11 '17 at 13:09
  • @Sergei how do you compare `cpprestdesk` with `boost.asio` which seems every C++ developer recommends. Seems like `cpprestdesk` is better suited for web scraping. – pouya Dec 20 '20 at 15:32

3 Answers3

50

All right, I'll try to point you in the right direction. Conceptually, a webcrawler is pretty simple. It revolves around a FIFO queue data structure which stores pending URLs. C++ has a built-in queue structure in the standard libary, std::queue, which you can use to store URLs as strings.

The basic algorithm is pretty straightforward:

  1. Begin with a base URL that you select, and place it on the top of your queue
  2. Pop the URL at the top of the queue and download it
  3. Parse the downloaded HTML file and extract all links
  4. Insert each extracted link into the queue
  5. Goto step 2, or stop once you reach some specified limit

Now, I said that a webcrawler is conceptually simple, but implementing it is not so simple. As you can see from the above algorithm, you'll need: an HTTP networking library to allow you to download URLs, and a good HTML parser that will let you extract links. You mentioned you could use wget to download pages. That simplifies things somewhat, but you still need to actually parse the downloaded HTML docs. Parsing HTML correctly is a non-trivial task. A simple string search for <a href= will only work sometimes. However, if this is just a toy program that you're using to familiarize yourself with C++, a simple string search may suffice for your purposes. Otherwise, you need to use a serious HTML parsing library.

There are also other considerations you need to take into account when writing a webcrawler, such as politeness. People will be pissed and possibly ban your IP if you attempt to download too many pages, too quickly, from the same host. So you may need to implement some sort of policy where your webcrawler waits for a short period before downloading each site. You also need some mechanism to avoid downloading the same URL again, obey the robots exclusion protocol, avoid crawler traps, etc... All these details add up to make actually implementing a robust webcrawler not such a simple thing.

That said, I agree with larsmans in the comments. A webcrawler isn't the greatest way to learn C++. Also, C++ isn't the greatest language to write a webcrawler in. The raw-performance and low-level access you get in C++ is useless when writing a program like a webcrawler, which spends most of its time waiting for URLs to resolve and download. A higher-level scripting language like Python or something is better suited for this task, in my opinion.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • I always though that C/C++ is the best programming languages. I am wrong all the time, what a shame. Thanks for providing such a useful answer to me. I will drop the idea, and try develop something else. By any change, do you have any idea, what programming language did google use? – popurity09 Nov 25 '10 at 19:18
  • 12
    @popurity09, well C++ is one of my favorite languages, but I don't think there is any such thing as the "best" programming language. Rather, certain languages are better suited for certain tasks than others. As for Google, all I know is that an early version of their webcrawler was written in Python. – Charles Salvia Nov 25 '10 at 19:34
  • 1
    i found this link helpful to get the concepts of building web spider http://code.google.com/p/driller-cpp-web-crawler/ – user63898 Apr 05 '11 at 10:57
  • 2
    I think a better exercise to learn C++ concepts would probably implementing some kind of a products database. Where each kind of product is a different class, each product inheriting from base class called Product. You could then higher level classes such as MilkProduct, MeatProduct etc. – Grim Apr 27 '11 at 10:28
  • 1
    The holy grail of web crawlers COULD be built in C++, but it'll require multithreading , and that's not easy. Agreed with the above statement about building one with Python. A lot of "magic" happens from within with python, of which you'd have to spell out with C++. – FredTheWebGuy Jun 07 '13 at 17:10
  • @CharlesSalvia i think C++ will miss Multi-threading capability which can be important for this project. – duckduckgo Jun 13 '13 at 06:29
  • 1
    @treemonster19, no C++ has multi-threading. C++11 has it standard, and with C++03 there are many available options. – Charles Salvia Jun 13 '13 at 16:24
  • @CharlesSalvia early versions of their web crawler were made in python which used to crash a lot. So, they made a new one with proper incremental indexing in C++. – DETSUP Jul 28 '21 at 17:51
5

Check this Web crawler and indexer written in C++ at: Mitza web crawler The code can be used as reference. Is clean and provides good start for a webcrawler codding. Sequence diagrams can be found at the above link pages.

Melvin
  • 77
  • 1
  • 10
user2195463
  • 321
  • 5
  • 8
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/faq#reputation) you will be able to [comment on any post](http://stackoverflow.com/privileges/comment). – Bohemian May 18 '13 at 05:25
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Kirk May 18 '13 at 05:40
  • 2
    And it *did* change! :/ – Samuel Bushi Jun 24 '15 at 09:31
0

A web-crawler has the following components in it:

  • Downloading an HTML file
  • Extracting links from it
  • Pushing all the links into a queue
  • {web indexing and ranking if necessary}
  • Repeating this with the front element of the queue

This one has it all Web-Crawler.

It would be very helpful for beginners to learn about complete understanding of a web-crawler, concepts of multithreading and web-ranking.

Anupam
  • 428
  • 7
  • 15
  • Multi-threaded web crawler? why not an asynchronous web crawler? although you can boost an asynchronous crawler by using threading. But many languages these days using event loops to handle async stuff – pouya Dec 20 '20 at 15:41
  • I am gonna make this simple for you. Pros of a multithreaded crawler: - Downloading multiple websites at the same time. - Parsing HTML pages at the same time. - And hence, a huge performance boost. But hey, there are some cons too: - Complex debugging. In the end, you have to decide if the language is providing necessary multithreading tools and if you can achieve maximum efficiency(time and space). – Anupam Dec 20 '20 at 16:09
  • Just a suggestion, drop multithreading and use an async lib to make multiple http requests concurrently (this is the blocking part of the app really) use any lib your currently using to parse html (Parsing html is not a CPU intensive operation). Then compare the results with your multithreaded solution. I bet you won't see any difference in the results. Done it before in python and node, CPP should be the same. Async is the way to go. – pouya Dec 20 '20 at 16:26