Slashdot Mirror


How To Build a Web Spider On Linux

IdaAshley writes, "Web spiders are software agents that traverse the Internet gathering, filtering, and potentially aggregating information for a user. This article shows you how to build spiders and scrapers for Linux to crawl a Web site and gather information, stock data, in this case. Using common scripting languages and their collection of Web modules, you can easily develop Web spiders."

25 of 104 comments (clear)

  1. Hmm... by joe_cot · · Score: 5, Funny

    Yes, but does it run on ... damn.

    1. Re:Hmm... by strstrep · · Score: 3, Interesting

      PHP lightweight? Ha!

      The PHP interpreter is over 5 megabytes in size. And it isn't thread-safe. That's a lot of memory overhead for a program that's going to be blocking on I/O most of the time, seeing how you'll have to fork() a new process for each new "thread" you want.

      Also, languages like Perl and Python have binaries that are about 1 megabyte in size. Now, while they'll probably need to load in extra files for most practical applications, these extra files are typically small. Most importantly, Perl and Python are thread-safe.

      Perl, for example, includes libraries such as Thread::Queue, which allows you to very easily create a threading model with worker threads, without having to worry too much about condition variables, mutexes, and the like.

      Disclaimer: All measurements done on x86 Debian Linux.

  2. Crawling efficiently by BadAnalogyGuy · · Score: 5, Informative

    Their example of a web crawler uses a queue to hold links. Since a link may appear twice, they use a lookup to scan the queue to see if the link is already loaded, and discard it if so.

    Better to use an associative array to cache the links since lookup is O(1). The Queue's lookup time is O(n) and if n gets large, so does the lookup time, not to mention that since you are checking each link the worst case scenario is a lookup time of O(n^2). A hash (associative array) will perform the same check in O(n). /([\W_\-]@\W+)/gs

    1. Re:Crawling efficiently by Anonymous Coward · · Score: 2, Insightful

      Python has a builtin set type. Have no idea why they did not use it.

    2. Re:Crawling efficiently by Mr2cents · · Score: 2, Interesting

      Maybe because they don't know the first thing about efficiency? You'd be surprised how much programmers don't know/care about efficiency. Once, incidentilly also on a crawler (student project), I improved the function reading a tree of URL's from 1 hour(!) to 0.1second! The guy tested it on an example with 10 URL's and it worked, but his implementation was O(n^2) and involved copying huge amounts of memory each step. Don't ask me how he thought this would be scalable.

      --
      "It's too bad that stupidity isn't painful." - Anton LaVey
    3. Re:Crawling efficiently by Zonk+(troll) · · Score: 2, Funny
      Maybe because they don't know the first thing about efficiency? You'd be surprised how much programmers don't know/care about efficiency.


      If you're surprised about programmers not knowing/caring about efficiency, do you actually use a computer?
      --
      "The Federal Reserve is a fraudulent system."--Lew Rockwell
      End The FED. -
  3. The 90s called by dave562 · · Score: 5, Funny

    They want their technology back.

  4. downloads by Bananatree3 · · Score: 4, Informative

    for those of us who don't have them, here are the basics:



    Wget: http://www.gnu.org/software/wget/.

    Curl http://curl.haxx.se/
  5. Hardly linux-specific by h_benderson · · Score: 5, Insightful

    All my love for linux aside, this has to do nothing with linux, the kernel (or even the GNU/Linux, the OS). It works just as well on any other unix-derivate or even windows.

  6. some points by cucucu · · Score: 5, Interesting
    • Don't forget to check and respect robots.txt. Python has a module that helps you parse that file
    • Samie and its Python port Pamie are your friends. You can automate IE so your script is treated as an human and not discriminated as a robot.
    • I use such beasts to do one-click time reporting at work and one-click cartoon collecting in my favorite newspaper.
    • And once I even repeatedly voted on an online poll and changed the course of history.
    • Ah, yes, TFA was about building a spider on Linux. I didn't check if my one-click IE scripts work on IE/Wine/Linux.
    • If I write an one-click script for online shopping, does it infringe the infamous Amazon patent?
    • When will Firefox's automation capabilities match those of IE?
    1. Re:some points by killjoe · · Score: 3, Informative

      "When will Firefox's automation capabilities match those of IE?"

      It's always had it. Look up XUL some day. The entire browser is written in xul.

      --
      evil is as evil does
  7. Re:yes, I did RTFA by Faylone · · Score: 4, Funny

    You RTFA? Are you sure you're in the right place?

  8. Oh sweet Jesus! by msormune · · Score: 3, Insightful

    Pull the article out. The last thing we need is more indexing bots.

  9. Re:Just what the internet needs... by ComaVN · · Score: 3, Informative

    I think that's robots.txt, *not* spider.txt

    --
    Be wary of any facts that confirm your opinion.
  10. crawling is not so trivial by cucucu · · Score: 2, Interesting
    As the two students who started a little web search company, crawling the web is not trivial: http://infolab.stanford.edu/~backrub/google.html. An excerpt follows.


    Running a web crawler is a challenging task. There are tricky performance and reliability issues and even more importantly, there are social issues. Crawling is the most fragile application since it involves interacting with hundreds of thousands of web servers and various name servers which are all beyond the control of the system.

    In order to scale to hundreds of millions of web pages, Google has a fast distributed crawling system. A single URLserver serves lists of URLs to a number of crawlers (we typically ran about 3). Both the URLserver and the crawlers are implemented in Python. Each crawler keeps roughly 300 connections open at once. This is necessary to retrieve web pages at a fast enough pace. At peak speeds, the system can crawl over 100 web pages per second using four crawlers. This amounts to roughly 600K per second of data. A major performance stress is DNS lookup. Each crawler maintains a its own DNS cache so it does not need to do a DNS lookup before crawling each document. Each of the hundreds of connections can be in a number of different states: looking up DNS, connecting to host, sending request, and receiving response. These factors make the crawler a complex component of the system. It uses asynchronous IO to manage events, and a number of queues to move page fetches from state to state.

    It turns out that running a crawler which connects to more than half a million servers, and generates tens of millions of log entries generates a fair amount of email and phone calls. Because of the vast number of people coming on line, there are always those who do not know what a crawler is, because this is the first one they have seen. Almost daily, we receive an email something like, "Wow, you looked at a lot of pages from my web site. How did you like it?" There are also some people who do not know about the robots exclusion protocol, and think their page should be protected from indexing by a statement like, "This page is copyrighted and should not be indexed", which needless to say is difficult for web crawlers to understand. Also, because of the huge amount of data involved, unexpected things will happen. For example, our system tried to crawl an online game. This resulted in lots of garbage messages in the middle of their game! It turns out this was an easy problem to fix. But this problem had not come up until we had downloaded tens of millions of pages. Because of the immense variation in web pages and servers, it is virtually impossible to test a crawler without running it on large part of the Internet. Invariably, there are hundreds of obscure problems which may only occur on one page out of the whole web and cause the crawler to crash, or worse, cause unpredictable or incorrect behavior. Systems which access large parts of the Internet need to be designed to be very robust and carefully tested. Since large complex systems such as crawlers will invariably cause problems, there needs to be significant resources devoted to reading the email and solving these problems as they come up.
  11. Quality of article? by interp · · Score: 2, Insightful

    I've never programmed in Ruby, but I think the comment in Listing 1 says it all:
    "Iterate through response hash"

    Why would somebody want to do that?
    A quick net search "reveals": A simple resp["server"] is all you need.
    Maybe the article was meant to be posted on thedailywtf.com?

  12. Re-inventing a square wheel by rduke15 · · Score: 5, Insightful

    Basically, the article gives you ruby and python examples of how to get web pages, and (badly) parse them for information. The same thing everyone has been doing for at least a decade with Perl and the appropriate modules, or whatever other tools, except that most know how to do it correctly.

    The first script is merely ridiculous: 12 lines of code (not counting empty and comment lines) to do:

    HEAD slashdot.org | grep 'Server: '

    But it gets worse. To extract a quote from a page, the second script suggests this:

    stroffset = resp.body =~ /class="price">/
    subset = resp.body.slice(stroffset+14, 10)
    limit = subset.index('<')
    print ARGV[0] + " current stock price " + subset[0..limit-1] +
    " (from stockmoney.com)\n"

    You don't need to know ruby to see what it does: it looks for the first occurence of 'class="price">' and just takes the 10 characters that follow. The author obviously never used that sort of thing for more than a couple of days, or he would know how quickly that will break and spit out rubbish.

    Finally, there is a Python script. At first glance, it looks slightly better. It uses what appears to be the Python equivalent of HTML::Parse to get links. But a closer look reveals that, to find links, it just gets the first attribute of any a tag and uses that as the link. Never mind if the 1st attribute doesn't happen to be "href".

    I suppose the only point of that article were the IBM links at the end:

    Order the SEK for Linux, a two-DVD set containing the latest IBM trial software for Linux from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.

    And that is in a section for Linux developers on the IBM site? Maybe the did copy stuff from SCO after all?...

    1. Re:Re-inventing a square wheel by rduke15 · · Score: 4, Insightful

      what exactly is HEAD slashdot.org

      It's a (perl) script which comes with libwww-perl which either is now part of the standard Perl distribution, or is installed by default in any decent Linux distribution.

      If you don't have HEAD, you can type a bit more and get the server with LWP::Simple's head() method (then you don't need grep):

      $ perl -MLWP::Simple -e '$s=(head "http://slashdot.org/" )[4]; print $s'

      Either way is better than those useless 12 lines of ruby (I'm sure ruby can also do the same in a similarly simple way, but that author just doesn't have a clue)

  13. Actually... by SanityInAnarchy · · Score: 3, Interesting

    Some websites do not have good search functionality. Sometimes it's an area that Google doesn't crawl (robots.txt and such), and sometimes I'm looking for something very, very specific.

    Regardless, I do, in fact, build spiders. For instance, in an MMO I play, all users can have webpages, so it's very useful to have a spider as part of a clan/guild/whatever to crawl the webpages looking for users who have illegal items and such. In a more general way, there is a third-party site which collects vital statistics of everyone who puts those in their user page, so you can get lists of the most powerful people in the game, the richest people, etc.

    --
    Don't thank God, thank a doctor!
  14. That reminds me. by archeopterix · · Score: 2, Informative
    Also, because of the huge amount of data involved, unexpected things will happen. For example, our system tried to crawl an online game. This resulted in lots of garbage messages in the middle of their game! It turns out this was an easy problem to fix.
    Unfortunately, many web developers still ignore the inevitable, leaving their sites vulnerable to the dreaded Googlebot "attack". While most of the spider developer manuals (TFA included) stress the importance of being polite (respect robots.txt & friends), most of the "become teh Web Master in x days" books don't even mention robots.txt. Go figure.

    For a good chuckle, see The Spider of Doom on the Daily WTF.

    And please use robots.txt.

    And go see Google Webmaster tools.

    And don't wear socks with sandals. Well, ok, this one is optional.

  15. It's a trap! by radu.stanca · · Score: 2, Funny

    Ah, I can see it clearly now!

    1. Post to Slashdot a decoy article(it includes Linux in the subjest) with new spam tricks
    2. Watch if spam increases 30% next days
    3. Bribe Cowboy Neal with 10G midget lesbian pr0n and get IP adresses of the art. readers
    4. Load shotgun and make the world a better place!

  16. Okay kids... by Balinares · · Score: 4, Informative
    Just so people who may come across this know, if you're going to do some HTML or XHTML parsing in Python, you'd be insane not to use BeautifulSoup or a similar tool.

    Example to find all links in a document:
    from BeautifulSoup import BeautifulSoup
    for tag in BeautifulSoup(html_document).findAll("a"):
      print tag["href"]
    Yes, it's that simple. For an URL opener that also handles proxies, cookies, HTTP auth, SSL and so on, look into the urllib2 module that ships natively with Python.
    --

    -- B.
    This sig does in fact not have the property it claims not to have.
  17. Re:Obligatory by k33l0r · · Score: 2, Funny

    Has there ever been a news story on Slashdot that doesn't have a "I, for one, welcome our new [Insert here] overlords" comment attached to it?

  18. Incorrect Title by OneSmartFellow · · Score: 2


    Should be: "How Not ..."

    I don't think I am alone in my thinking

  19. Re:Okay kids...(in Ruby) by amran · · Score: 2, Interesting

    I couldn't resist - in Ruby, using the beautiful (but much understated) hpricot library:

    doc = Hpricot(open(html_document))
    (doc/"a").each { |a| puts a.attributes['href'] }

    Check it out - I've been using it for a project, and it's really fast and really easy to use (supports both xpath and css for parsing links). For spidering you should check out the ruby mechanize library (which is like perl's www-mechanize, but also uses hpricot, making parsing the returned document much easier).