2013-06-25 26 views
0

嗨,我需要獲取所有的數據頁面。如果照片和每個主題的名字。 該頁面是herec#windows phone中的網頁廢料

我知道我有兩種選擇。有了這個,我只能得到整個頁面的圖像。但是,如果有人知道互補趕上一切都將是最好的方式:

int startIndex = e.Result.IndexOf(@"><img"); 
string result = e.Result;    
result = e.Result.Substring(startIndex, e.Result.Length - startIndex); 
startIndex = result.IndexOf(".php?src=") + 9; 
int endIndex = result.IndexOf(".jpg", startIndex); 
string link = result.Substring(startIndex, endIndex - startIndex) + ".jpg"; 
MessageBox.Show(link); 
imagem.Source = new BitmapImage(new Uri(link)); 

另一種方式是這樣的。我創建了一個類來存放數據並創建一個列表,但字符串「pattern」必須完全錯誤。因爲我不喜歡騎這種類型的字符串。從另一個話題只是複製並試圖創建自己的基於此:

private void ConsultaPopularVideos(string uri) 
     { 
      WebClient web2 = new WebClient(); 
      web2.DownloadStringAsync(new Uri(uri)); 
      web2.DownloadStringCompleted += web2_DownloadStringCompleted; 
     } 

     void web2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      if (!e.Cancelled && e.Error == null && !String.IsNullOrEmpty(e.Result)) 
      { 
       _popVideos = new List<PopularVideos>(); 
       // Aqui você pega todos os links da página 
       // P.S.: Se a página mudar, você tem que alterar o pattern aqui. 
       string pattern = @"\<a\shref\=[\""|\'](?<url>[^\""|\']+)[\""|\']\stitle\=[\""|\'](?<title>[^\""|\']+).php?src=[\""|\'](?<img>[^\""|\']+)[\""|\']\s\width='275'"; 


       // Busca no HTML todos os links 
       MatchCollection ms = Regex.Matches(e.Result, pattern, RegexOptions.Multiline); 


       Debug.WriteLine("----- OK {0} links encontrados", ms.Count); 

       foreach (Match m in ms) 
       { 
        // O pattern acima está dizendo onde fica o Url e onde fica o nome do artista 
        // e esses são resgatados aqui 
        Group url = m.Groups["url"]; 
        MessageBox.Show(m.Groups.ToString()); 
        Group title = m.Groups["title"]; 
        Group img = m.Groups["img"]; 

        if (url != null && title != null && img != null) 
        { 
         //Debug.WriteLine("author: {0}\nUrl: {1}", author.Value, url.Value); 

         // Se caso tenha encontrado o link do artista (pois há outros links na página) continua 
         if (url.Value.ToLower().IndexOf("/") > -1) 
         { 
          // Adiciona um objeto Artista à lista 
          PopularVideos video = new PopularVideos(title.Value, url.Value, img.Value); 
          _popVideos.Add(video);        
         } 
        } 
       } 
       listBoxPopular.ItemsSource = _popVideos; 
      } 
     } 

類:

class PopularVideos 
    { 
     public PopularVideos() { } 
     public PopularVideos(string nome, string url, string img) 
     { 
      Nome = nome; 
      Url = new Uri(url); 
      BitmapImage Img = new BitmapImage(new Uri(img)); 
     } 
     public string Nome { get; set; } 
     public string Img { get; set; } 
     public Uri Url { get; set; } 
    } 

回答

0

使用正則表達式報廢從網頁數據並不是一個很好的解決方案,因爲這將是不可靠的,脆弱和不易實現。 我會推薦使用[htmlagilitypack] [http://htmlagilitypack.codeplex.com/]來取消數據,它是一個成熟的庫,支持windows phone,我在我的windows phone應用程序中使用了這個工具,並且非常滿意它。

+0

我試過使用它,我不能,也許你可以幫助我與這個網站和我需要的數據。好?我嘗試使用時總是遇到問題。或根據我尋求的例子不正確的dll或函數 –

+0

你能描述你得到的問題嗎? – peanut