Vue d'ensemble
Voici quelques notes sur les bases de la chenille.
It is a console app - It doesn't need a rich interface, so I figured a console application would do. The output is done as an html file and the input (what site to view) is done through the app.config. Making a windows app out of this seemed like overkill.
The crawler is designed to only crawl the site it originally targets. It would be easy to change that if you want to crawl more than just a single site, but that is the goal of this little application.
Originally the crawler was just written to find bad links. Just for fun I also had it collect information on page and viewstate sizes. It will also list all non-html files and external urls, just in case you care to see them.
The results are shown in a rather minimalistic html report. This report is automatically opened in Internet Explorer when the crawl is finished.
Obtenir le texte d'une page HTML
La première pièce cruciale de la construction d'un robot est le mécanisme pour sortir et aller chercher le code html hors de la bande (ou votre machine locale, si vous avez la site fonctionnant localement.). Comme beaucoup d'autres choses, .NET a des classes pour faire ceci même chose dans le cadre.
private static string GetWebText(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.UserAgent = "A .NET Web Crawler";
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string htmlText = reader.ReadToEnd();
return htmlText;
}
La classe HttpWebRequest peut être utilisée pour demander n'importe quelle page d'Internet. La réponse (récupérée via un appel à GetResponse()) contient les données que vous voulez. Obtenez le flux de réponse, lancez-le dans un StreamReader, et lisez le texte pour obtenir votre code HTML. pour référence: font http://www.juicer.headrun.com
Êtes-vous cela pour coups de pied? Parce qu'il y a beaucoup de crawlers web pré-faits gratuits. – Fragsworth