Je suis assez nouveau dans le pack d'agilité HTML, j'ai donc besoin d'aide pour savoir où aller ensuite. Je peux faire des choses simples comme extraire une valeur d'un href (connaître la chaîne d'URL que je cherchais) et je peux extraire comme la valeur dans un intervalle basé sur une classe spécifique qui était utilisée. Mais je ne comprends pas comment utiliser le pack d'agilité HTML dans une situation où il y a une tonne de balises ou trois, n'est-ce pas une véritable ancre solide à laquelle se lier?
Voici un morceau de code que je suis en train de parcourir. J'ai placé des données factices dans les cellules pour démontrer ce que je recherche.
Quelle est la meilleure façon d'extraire les éléments suivants:
1.) Nom de l'entreprise?
2.) Numéro de téléphone?
3.) Adresse e-mail?
HTML ....
<td>
<!-- Company Info -->
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="black">
<table cellspacing="1" cellpadding="0" border="0" width="370">
<tr>
<th>COMPANY NAME</th>
</tr>
<tr>
<td class="search">
<table cellpadding="5" cellspacing="0" border="0" width="100%">
<tr>
<td>
<table cellpadding="1" cellspacing="0" border="0" width="100%">
<tr>
<td colspan="2" align="center">Un-needed Links...</td>
</tr>
<tr>
<td align="center" colspan="2"><hr></td>
</tr>
<tr>
<td align="right" nowrap>
<b>
<font color="FF0000">
Contact Person
<img src="/images/icon_contact.gif" align="absmiddle"> :
</font>
</b>
</td>
<td align="left" width="100%"> Judy Smith</td>
</tr>
<tr>
<td align="right" nowrap>
<b><font color="FF0000">Phone Number <img src="/images/icon_phone.gif" align="absmiddle"> :</font></b></td>
<td align="left" width="100%"> 555-555-5555</td>
</tr>
<tr>
<td align="right" nowrap><b><font color="FF0000">E-mail Address <img src="/images/icon_email.gif" align="absmiddle"> :</font></b></td>
<td align="left" width="100%"> <a HREF="mailto:judy.smith@companyname.com">judy.smith@companyname.com</a></td>
</tr>
<tr>
<td align="center" colspan="2"><hr></td>
</tr>
<tr>
<td align="right" nowrap><b><font color="FF0000">Home Office Location <img src="/images/icon_home.gif" align="absmiddle"> :</font></b></td>
<td align="left" width="100%"> ATLANTA, GA</td>
</tr>
<tr>
<td align="right" nowrap><b><font color="FF0000">Home Office Phone <img src="/images/icon_home.gif" align="absmiddle"> :</font></b></td>
<td align="left" width="100%"> 555-555-5555</td>
</tr>
<tr>
<td align="right" nowrap><b><font color="FF0000">Home Office Fax <img src="/images/icon_home.gif" align="absmiddle"> :</font></b></td>
<td align="left" width="100%"> 666-666-6666</td>
</tr>
<tr>
<td align="center" colspan="2"><hr></td>
</tr>
<tr>
<td align="right" nowrap><b><font color="FF0000">Broker MC Number <img src="/images/icon_number.gif" align="absmiddle"> :</font></b></td>
<td align="left" width="100%"> 123456</td>
</tr>
<tr>
<td align="right" nowrap><b><font color="FF0000">Carrier MC Number <img src="/images/icon_number.gif" align="absmiddle"> :</font></b></td>
<td align="left" width="100%"> 654321</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<br>
<!-- Starting Point -->
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="black">
<table cellspacing="1" cellpadding="0" border="0" width="370">
<tr>
<th>Starting Point</th>
<th>Available</th>
</tr>
<tr>
<td class="search" width="270"> <b>ABBEVILLE, GA </b></td>
<td class="search" align="center" width="100"><span style="color: forestgreen"> 1/5/11 </span></td>
</tr>
</table>
</td>
</tr>
</table>
<br>
<!-- Destination Point -->
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="black">
<table cellspacing="1" cellpadding="0" border="0" width="370">
<tr>
<th>Destination Point</th>
<th>Direction</th>
</tr>
<tr>
<td class="search" width="270"> <b>ATLANTA, GA </b></td>
<td class="search" align="center" width="100"><span style="color: FF0000"> </span></td>
</tr>
</table>
</td>
</tr>
</table>
<br>
<!-- Truck Details -->
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="black">
<table cellspacing="1" cellpadding="0" border="0" width="370">
<tr>
<th>Truck Details</th>
</tr>
<tr>
<td class="search">
<table cellpadding="5" cellspacing="0" border="0">
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="right"><b>Date Posted :</b></td>
<td align="left"> 1/5/2011 10:34:48 AM</td>
</tr>
<tr>
<td align="right"><b>Quantity :</b></td>
<td align="left"> 1</td>
</tr>
<tr>
<td align="right"><b>Equipment Type :</b></td>
<td align="left"> FT</td>
</tr>
<tr>
<td align="right"><b>Load Size :</b></td>
<td align="left"> Full</td>
</tr>
<tr>
<td align="right" valign="top"><b>Special Information :</b></td>
<td align="left"> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<br>
</td>
.... Plus HTML
Eh bien, vous devez comprendre XPATH pour vraiment profiter des capacités de grattage du pack d'agilité HTML :-) Vous pouvez commencer par Google sur des exemples XPATH .
En se concentrant sur la question du grattage d'écran, la partie délicate consiste à sélectionner ce que vous pensez être l'expression xpath la plus discriminante pour les informations que vous souhaitez obtenir. La plupart du temps, il n'y a pas qu'une seule solution, et vous devez être prêt à mettre à jour votre code pour suivre l'évolution HTML du site cible.
C'est donc un compromis entre des expressions très simples avec un risque qu'elles correspondent à des textes indésirables, et des expressions trop discriminantes, non tolérantes aux évolutions du HTML gratté, avec un risque qu'elles ne correspondent à rien.
Quant à votre texte spécifique, c'est un bon exemple du monde réel, et voici un code qui le fait:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(yourText);
string companyName = doc.DocumentNode.SelectSingleNode("/td/table/tr/td/table/tr/th").InnerText;
Console.WriteLine("company name=" + companyName);
// another way
companyName = doc.DocumentNode.SelectSingleNode("//td[@class='black']/table/tr/th").InnerText;
Console.WriteLine("company name=" + companyName);
// a more advanced XPATH expression, means
// "Select a TD tag anywhere in the doc that has a preceding sibling of TD type with a B chid, with a FONT child with inner text starting with 'Phone Number'"
string phoneNumber = doc.DocumentNode.SelectSingleNode("//td[starts-with(preceding-sibling::td/b/font/text(), 'Phone Number')]").InnerText;
Console.WriteLine("phone Number=" + phoneNumber);
// same kind of story but go down the next A tag
string email = doc.DocumentNode.SelectSingleNode("//td[starts-with(preceding-sibling::td/b/font/text(), 'E-mail')]/a").InnerText;
Console.WriteLine("email=" + email);
PS : veuillez noter que le pack d'agilité HTML s'attend toujours à ce que les balises utilisées dans les expressions XPATH soient en minuscules, même si elles ne sont pas dans le texte HTML d'origine.
Comme vous le voyez, le nom de l'entreprise est récupéré ici à l'aide de deux expressions différentes. Ils travaillent tous les deux sur l'échantillon, mais le premier ne résistera pas si une nouvelle étiquette est ajoutée n'importe où au milieu. Le second est plus évolutif mais est basé sur une balise de classe CSS qui peut également changer. C'est toujours un compromis.
Le numéro de téléphone et l'e-mail sont similaires mais montrent la puissance de XPATH.