Since the venture has tried several entrepreneurial projects, every time bootstrap, often need to use some content on the Internet, where it is inevitable to write some simple crawler to crawl some data to complete the initial project guidance. These little reptiles for me to learn. Net,http Protocol, Framework design and design patterns provide a lot of help. The reptilian version of a refactoring and upgrade are often able to deepen my knowledge of certain areas.
The most famous reptile items in Open source are Nutch and Heritrix. Nutch is a pedigree, Lucene, Hadoop, HBase and so on to Nutch. Heritrix configuration A bit troublesome, I downloaded the source code, but just about a bit, there is no specific debug its specific source. A few days ago also saw the garden Arrowcat and Phinecos (cave scattered people) two comrades did a little introduction. As individuals do not have this in-depth demand, so always rely on their own individual to think how to complete a simple crawler design and coding. These two projects are written in Java, so it is still not convenient for many of our. Net fans. To this end, I am in the process of writing a reptile encountered problems, solutions especially some of the needs of the summary for you to do some basic introduction, hoping to help some people, I personally is a good summary.
First, request to send. I usually write the crawler is the HTTP protocol, of course, you can use the TCP protocol to simulate HTTP request.
· HTTP request send and Response accept
The two-class encapsulation of the HttpWebRequest and HttpWebResponse in the. Net framework makes it easy to send HTTP, but these two classes are designed to be very knowledgeable about HTTP protocol, so unfamiliar comrades need to dig deeper. Typically, get and post two methods are provided. Here may appear gzip compression decompression, some sites for useragent requirements can not be empty and so on.
· How to encode a website
Our common coding method is generally UTF-8,GBK or GB2312 code, here if the site changes more need to automatically detect the encoding of each site, or the coding is wrong, the consequences are very serious. This I have not done, should take a request response and then analyze the results of the HTTP header can be determined. If the site can be simply enumerated, you can set it manually.
· Request Send Frequency
If you request too much, easy to the other machine load, may be blocked by some software server, the most exaggerated will let the other administrator to find, so be careful to set a suitable request delay. Do not know the spider of Google's parameters how to get, it should also be the accumulation of experience slowly.
· HTTP analysis: Most of the time you write spider before you need to analyze the source of the crawler, here you need to use the tools to complete the task, I commonly used is httpwatch,fiddler2, IE Developer Toolbar, FireBug, similar to some, For example, Microsoft Network Monitor, Web Development Helper, and so on. With the help of these tools, you can quickly understand the structure of the Web page and the details of the request.
Second, content parsing. After we get the response, we need to parse it according to the format of the content. I've written about Hotmail, Gmail, Yahoo!. Mail, 163 's contact importer, which may require you to parse XML, JSON, and other formats. There is also a blog about the RSS parsing problem, here are some rules, if you can find the open source of the best library, there is no have to read the point of the document or study its rules to try to parse. For XML parsing, using Sax is much faster than DOM. Rss+atom may have the. NET aspect of Open source class library, I used to parse, this is difficult to all the situation cover, risk or quite high. For the most common HTML parsing you can choose a class library such as Htmlparser, and of course you can start from scratch, I use regular expressions and the. Net framework to fix these problems, such as extracting all the HTML anchors from the document, Remove all HTML tags, disguised as the path URL into an absolute path URL, intercept part of the content to a sentence end. Also may encounter the situation is the vertical search, for instance catches the movie information, May to the director, the actor, the playback length, the picture request is very accurate, here your spider extractor need to rely on each other's HTML format, if the other party has the RSS and so on the API that is cool. No, you can only find ways to reduce reliance on HTML formats, or you may need to provide some alarm or logging mechanisms that fail the parser.
List the code you might need:
Extract All Links:
Regex htmlLinkReg = new Regex(@"(href)[ ]*=[ ]*[""'][^""'> ]+[""'> ]", RegexOptions.IgnoreCase);
Get rid of HTML tags and encodings like this:
result = Regex.Replace(html, @"<(.[^>]*)>?", replacedWord, RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"&\w{2,7};", "");
Third, the distributed architecture. This may involve the deployment of multiple clients, because many times the IP is a rare resource, the same IP to catch the speed is very slow, and dare not too fast. Oh. I wrote a distributed crawler, grabbed tens of millions of of data down. Client I usually write with WinForm, background open point thread to complete a variety of crawling tasks, it every time from the server to get a little task, and then to send request, analytic results, and finally through HTTP request to upload crawling results. This simple architecture is easy to scale.
· Crawl history: You may need to keep the spider URL Crawl history on one hand to avoid repeated crawling, and you can also determine whether the URLs were updated after the last crawl.
· Website update: If you need to continue to crawl the content of a site, you may need to the home page or update the relevant pages as the source, and then extract all the links to the page, and then filter out the page you want to parse and have crawled the page, and then use a queue to store these sources, By further deepening the reptile depth to find all possible new content pages, you can use breadth-first search or DFS to recursively drill down. Here you may encounter a mismatch between the input and output rates, you may need to rely on some queue simulations, and of course you can do it yourself through the database.
· Specific page crawling: If you store the source of specific content pages through databases, for distributed applications, you may need to consider synchronization issues that you can rely on. NET provides some concurrency methods to resolve possible concurrent duplication problems, such as monitor, semaphore, Event, mutex, and so on.
The above points are my summary of possible problems with a simple crawler design. I hope it's helpful for you.