2011-11-13 17 views
6

मैं साइट से कुछ डेटा स्क्रैप करने के लिए एचटीएमएल एजिलिटी पैक का उपयोग करने की कोशिश कर रहा हूं। मैं वास्तव में एक फोरच के अंदर selectnodes का उपयोग करने और फिर सूची या सरणी में डेटा निर्यात करने के तरीके को समझने में संघर्ष कर रहा हूं।एचटीएमएल एजिलिटी पैक नोड्स का चयन करें

यहां कोड है जो मैं अभी तक काम कर रहा हूं।

 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. 

     } 

मेरे पास उपरोक्त कोड में टिप्पणियां हैं जो दिखाती हैं कि क्या काम करता है और मैं क्या हासिल करना चाहता हूं और किस तरह से काम करना चाहता हूं।

यदि किसी के पास कोई सुझाव है या पढ़ना अच्छा होगा! मैं फ़ोरम और उदाहरण खोज रहा हूं और जो कुछ भी मैं उपयोग कर सकता हूं उसके पार नहीं आया है।

उत्तर

11

टिप्पणी के साथ आपकी पहली समस्या SelectNodes काम नहीं करती है क्योंकि 'आईडी' तत्व तत्व नहीं है, यह एक विशेषता नाम है। आपने विशेषता का चयन करने और मूल्य की तुलना करने के लिए अपने अन्य अभिव्यक्तियों में सही वाक्यविन्यास का उपयोग किया है। उदाहरण के लिए, //ElementName[@attributeName='value']। मुझे लगता है कि [attributeName='value'] भी काम करना चाहिए, लेकिन मैंने इसका परीक्षण नहीं किया है।

SelectNodes फ़ंक्शन के अंदर वाक्यविन्यास को "XPath" कहा जाता है। This link आपकी मदद कर सकता है।

seller नोड जिसे आप चुन रहे हैं वर्तमान पुनरावृत्ति के लिए node का एक भाई है जो एक alt विशेषता के साथ एक img है। हालांकि मुझे लगता है कि आप चाहते हैं कि सही वाक्यविन्यास सिर्फ img[@alt] है।

अगली समस्या जहां आप कहते हैं कि यह संकलित नहीं होगा, त्रुटि संदेश जांचें, यह शायद वापस तर्क प्रकारों की शिकायत करेगा। sellers.Add मुझे लगता है कि एक और एचटीएमएल नोड नाम देने की तलाश है, न कि एक विशेषता जो कि जोड़ के अंदर अभिव्यक्ति लौट रही है।

इसके अलावा, एचटीएमएल एजिलिटी पैक डॉक्स और सिंटैक्स से संबंधित अन्य प्रश्नों को देखें।

संबंधित मुद्दे