1

我正在尝试从 HTML 页面获取链接和另一个元素,但我真的不知道该怎么做。这就是我现在所拥有的:

var client = new HtmlWeb(); // Initialize HtmlAgilityPack's functions.
var url = "http://p.thedgtl.net/index.php?tag=-1&title={0}&author=&o=u&od=d&page=-1&"; // The site/page we are indexing.
var doc = client.Load(string.Format(url, textBox1.Text)); // Index the whole DB.
var nodes = doc.DocumentNode.SelectNodes("//a[@href]"); // Get every url.

string authorName = "";
string fileName = "";
string fileNameWithExt;

foreach (HtmlNode link in nodes)
{
    string completeUrl = link.Attributes["href"].Value; // The complete plugin download url.

    #region Get all jars

    if (completeUrl.Contains(".jar")) // Check if the url contains .jar
    {
        fileNameWithExt = completeUrl.Substring(completeUrl.LastIndexOf('/') + 1); // Get the filename with extension.
        fileName = fileNameWithExt.Remove(fileNameWithExt.LastIndexOf('.')); ; // Get the filename without extension.
        Console.WriteLine(fileName);
    }

    #endregion

    #region Get all Authors

    if (completeUrl.Contains("?author=")) // Check if the url contains .jar
    {
        authorName = completeUrl.Substring(completeUrl.LastIndexOf('=') + 1); // Get the filename with extension.
        Console.WriteLine(authorName);
    }

    #endregion
}

我试图让所有文件名和作者彼此相邻,但现在一切都像随机放置,为什么?

有人可以帮我弄这个吗?谢谢!

4

1 回答 1

1

如果您查看 HTML,很遗憾它的格式不正确。有很多打开的标签,HAP 的结构方式不像浏览器,它将文档的大部分解释为深度嵌套。因此,您不能像在浏览器中那样简单地遍历表的行,它比这要复杂得多。

在处理此类文档时,您必须对查询进行相当多的更改。而不是搜索子元素,您必须搜索调整变化的后代。

var title = System.Web.HttpUtility.UrlEncode(textBox1.Text);
var url = String.Format("http://p.thedgtl.net/index.php?title={0}", title);

var web = new HtmlWeb();
var doc = web.Load(url);

// select the rows in the table
var xpath = "//div[@class='content']/div[@class='pluginList']/table[2]";
var table = doc.DocumentNode.SelectSingleNode(xpath);

// unfortunately the `tr` tags are not closed so HAP interprets
// this table having a single row with multiple descendant `tr`s
var rows = table.Descendants("tr")
    .Skip(1); // skip header row

var query =
    from row in rows
    // there may be a row with an embedded ad
    where row.SelectSingleNode("td/script") == null
    // each row has 6 columns so we need to grab the next 6 descendants
    let columns = row.Descendants("td").Take(6).ToList()
    let titleText = columns[1].Elements("a").Select(a => a.InnerText).FirstOrDefault()
    let authorText = columns[2].Elements("a").Select(a => a.InnerText).FirstOrDefault()
    let downloadLink = columns[5].Elements("a").Select(a => a.GetAttributeValue("href", null)).FirstOrDefault()
    select new
    {
        Title = titleText ?? "",
        Author = authorText ?? "",
        FileName = Path.GetFileName(downloadLink ?? ""),
    };

所以现在您可以遍历查询并为每一行写出您想要的内容。

foreach (var item in query)
{
    Console.WriteLine("{0} ({1})", item.FileName, item.Author);
}
于 2012-07-14T21:26:16.807 回答