【问题标题】:scrab urls from web page using HtmlAgilityPack使用 HtmlAgilityPack 从网页抓取网址
【发布时间】:2017-05-26 01:25:16
【问题描述】:

这是我目前的代码

 foreach (var listBoxItem in listBox_google_urls.Items)
        {              
            var document = new HtmlWeb().Load(listBoxItem.ToString());
            var files = document.DocumentNode.Descendants("a").Select(a => a.GetAttributeValue("href", ".mp3")).Where(h => h.Contains(".mp3")).ToArray(); 
            listbox_urls.Items.AddRange(files);
        }      

这就是 listBox_google_urls.Items

 web_search.Navigate("https://www.google.com/search?q=" + val + "+(mp3|wav|ac3|ogg|flac|wma|m4a) -inurl:(jsp|pl|php|html|aspx|htm|cf|shtml) intitle:index.of -inurl:(listen77|mp3raid|mp3toss|mp3drug|index_of|wallywashis)");
        var search_results = this.web_search.Document.Links.Cast<HtmlElement>().Select(a => a.GetAttribute("href")).Where(h => h.Contains("http://")).ToArray();
        listBox_google_urls.Items.AddRange(search_results);

listBoxItem.ToString() 输出example

问题是这种方法有效,但只能抓取链接的标题 他们是我如何解决它的方法?已经谢谢了

【问题讨论】:

  • 你能发布一个 listBoxItem.ToString() 输出的例子吗?会更容易帮助你
  • @MarkRedfern 我编辑你可以检查一下
  • 您是要获取链接文本还是链接 URL?
  • 这样的url链接我想提取网页上的所有url都是这样的78.140.251.40/tmp_audio/top100/rr/…@MarkRedfern

标签: c# html html-agility-pack


【解决方案1】:

您的代码看起来不错,只是不确定为什么您默认为 ".mp3" 然后返回所有具有 ".mp3" 的代码?你最终会得到一组有效的.mp3 URL,然后是一大堆“.mp3”字符串?我刚刚进入了一个rando google搜索页面,并在href属性中查找了所有带有“mail”一词的url,这是结果

希望这能回答您的问题。如果您可以提供更多信息,也许我可以提供更多帮助

试试这个

        var document = new HtmlWeb().Load("http://s1.mymrmusic2.com/hmusic/Album/Foreign%20Albums/VA%20-%20Billboard%20Hot%20100%20(02%20April%202016)/VA%20-%20Billboard%20Hot%20100%20(02%20April%202016)%20%5B320%5D/");
        var files = document.DocumentNode.Descendants("a")
            .Where(a => !string.IsNullOrEmpty(a.GetAttributeValue("href", string.Empty)) && a.GetAttributeValue("href", string.Empty).Contains(".mp3"))
            .Select(a => new
            {
                Link = a.GetAttributeValue("href", string.Empty),
                Text = a.FirstChild.InnerText
            }).ToList();

也许试试这个选项

foreach (var listBoxItem in listBox_google_urls.Items)
        {
            var document = new HtmlWeb().Load(listBoxItem.ToString());
            var files = document.DocumentNode.Descendants("a")
                .Select(a => a.GetAttributeValue("href", ".mp3"))
                .Where(h => h.Contains(".mp3"))
                .Select(a => listBoxItem.ToString() + a).ToArray();
            listbox_urls.Items.AddRange(files);
        }

【讨论】:

  • 这并不能解决问题,但感谢您的帮助
  • @AdamKim - 这就是你所追求的吗?检查编辑的答案
  • 这就是我用我的代码得到的确切信息,我想得到像这样s1.mymrmusic2.com/hmusic/Album/Foreign%20Albums/… 这样的网址,而不是它们的标题,抱歉造成误解
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-10
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 2020-06-18
相关资源
最近更新 更多