Come posso ottenere tr link e contenuti con HtmlAgilityPack?

c# html-agility-pack html-parsing parsing

Domanda

Io uso questo codice per ottenere il sorgente html di url con HtmlAgilityPack:

private string GetUrlSource(string urlAddress)
        {
            string content = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = null;
                if (response.CharacterSet == null)
                    readStream = new StreamReader(receiveStream);
                else
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                content = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
            }
            return content;
        }

quindi, usa questo codice per ottenere i dati:

private string GetUrlSource(string urlAddress)
        {
            string content = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = null;
                if (response.CharacterSet == null)
                    readStream = new StreamReader(receiveStream);
                else
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                content = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
            }
            return content;
        }

e il mio risultato per i nodi sono:

private string GetUrlSource(string urlAddress)
        {
            string content = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = null;
                if (response.CharacterSet == null)
                    readStream = new StreamReader(receiveStream);
                else
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                content = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
            }
            return content;
        }

Io uso questo codice per ottenere i dati:

private string GetUrlSource(string urlAddress)
        {
            string content = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = null;
                if (response.CharacterSet == null)
                    readStream = new StreamReader(receiveStream);
                else
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                content = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
            }
            return content;
        }

Come posso ottenere link, titolo ed editore di ogni tag tr senza se e altro? ad esempio: http://link1.com , Title1 , Publisher1 e http://link2.com , Title2 , Publisher2 e http://link3.com , Title3 , Publisher3

Risposta accettata

Uno dei tanti modi possibili:

//select <tr> having child node <td>
var tr = doc.DocumentNode.SelectNodes("//div[@class='linear-view']/table/tr[td]");
foreach (HtmlNode node in tr)
{
    //select <td> having child node <a>
    var td1 = node.SelectSingleNode("./td[a]"); //or using index: ./td[1]
    var link = td1.FirstChild.Attributes["href"].Value;
    var title = td1.InnerText;
    //select <td> not having child node <a>
    var publisher = node.SelectSingleNode("./td[not(a)]") //using index: ./td[2]
                        .InnerText;
}



Autorizzato sotto: CC-BY-SA with attribution
Non affiliato con Stack Overflow
È legale questo KB? Sì, impara il perché
Autorizzato sotto: CC-BY-SA with attribution
Non affiliato con Stack Overflow
È legale questo KB? Sì, impara il perché