2011-11-13 12 views
6

Sto cercando di utilizzare il pacchetto Agilità HTML per analizzare alcuni dati da un sito. Sto davvero cercando di capire come usare selectnode all'interno di un foreach e quindi esportare i dati in un elenco o array.Pacchetto agilità HTML Seleziona nodi

Ecco il codice con cui sto lavorando finora.

 string result = string.Empty; 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(http://www.amazon.com/gp/offer-listing/B002UYSHMM/); 
     request.Method = "GET"; 

     using (var stream = request.GetResponse().GetResponseStream()) 
     using (var reader = new StreamReader(stream, Encoding.UTF8)) 
     { 
      result = reader.ReadToEnd(); 
     } 

     HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
     doc.Load(new StringReader(result)); 
     HtmlNode root = doc.DocumentNode; 

     string itemdesc = doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']").InnerText; //this works perfectly to get the title of the item 
     //HtmlNodeCollection sellers = doc.DocumentNode.SelectNodes("//id['bucketnew']/div/table/tbody/tr/td/ul/a/img/@alt");//this does not work at all in getting the alt attribute from the seller images 
     HtmlNodeCollection prices = doc.DocumentNode.SelectNodes("//span[@class='price']"); //this works fine getting the prices 
     HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='resultsset']/table/tbody[@class='result']/tr"); //this is the code I am working on to try to collect each tr in the result. I then want to eather add each span.price to a list from this and also add each alt attribute from the seller image to a list. Once I get this working I will want to use an if statement in the case that there is text for the seller name instead of an image. 

     List<string> sellers = new List<string>(); 
     List<string> prices = new List<string>(); 

     foreach (HtmlNode node in nodes) 
     { 
      HtmlNode seller = node.SelectSingleNode(".//img/@alt"); // I am not sure if this works 
      sellers.Add(seller.SelectSingleNode("img").Attributes["alt"]); //this definitly does not work and will not compile. 

     } 

devo commenti nel codice di cui sopra che mostra ciò che funziona e ciò che non e una sorta di ciò che voglio non da realizzare.

Se qualcuno ha qualche suggerimento o lettura che sarebbe bello! Ho cercato forum ed esempi e non ho mai trovato nulla che potessi usare.

risposta

11

Il tuo primo problema con il commento SelectNodes non funziona perché "id" non è un nome di elemento, è un nome di attributo. Hai utilizzato la sintassi corretta nelle altre espressioni per selezionare un attributo e confrontare il valore. Ad esempio, //ElementName[@attributeName='value']. Penso che anche [attributeName='value'] dovrebbe funzionare, ma non l'ho provato.

La sintassi all'interno della funzione SelectNodes è denominata "XPath". This link potrebbe aiutarti.

Il nodo seller che si sta selezionando è un fratello di node per l'iterazione corrente che è un img con un attributo alt. Comunque penso che la sintassi corretta che desideri sia solo img[@alt].

Il prossimo problema in cui si dice che non verrà compilato, controllare il messaggio di errore, probabilmente si lamenterà tipi di argomenti back. sellers.Add Penso che stia cercando di nominare un altro HtmlNode, non un attributo che è ciò che restituisce l'espressione all'interno dell'aggiunta.

Inoltre, controlla i documenti del pacchetto Agility Html e altre domande sulla sintassi.

Problemi correlati