2015-04-27 39 views
2

今天我遇到了一個很大的問題,幾個小時後我一直在尋找很多不同的方法,但他們都沒有爲我工作。我可以取出用戶名而不是href鏈接嗎?

我已經試過

  1. 獲取函數關係
  2. 正則表達式
  3. HTML敏捷性包

的問題是在VB.NET,我要搶了影片的片名並忽略之前的HTML鏈接。但問題是我不能,因爲每個標題的鏈接都改變了,我不明白Regex爲它創建代碼。

這裏是代碼和說Movie Link 1是我想抓的部分。

<a href="/download/fast-and-furious-7-2015-hd-ts-xvid-ac3-hq-hive-cm8-t10472303.html" class="cellMainLink">**Movie Link 1**</a> 

當然還有其他的標題我也需要去抓。所以我得到的代碼是這個,它不工作。

Dim r As New System.Text.RegularExpressions.Regex("class=""cellMainLink"">(?<name>.*)</a>") 
    Dim matches As MatchCollection = r.Matches(rssourcecode) 


    For Each itemcode As Match In matches 
     ListBox1.Items.Add(itemcode.Groups(2).Value) 
    Next 

對任何能夠幫助我的人,請儘快回覆我。

謝謝。

+0

[你不能用正則表達式解析HMTL](http://stackoverflow.com/questions/1732348/regex-match-open-標籤 - 除了-XHTML-自足標籤/ 1732454#1732454)。 –

+0

我從標題中刪除了標籤:http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles – Jens

回答

0

使用HTML Agility Pack您可以使用此代碼:

Dim links As New List(Of String)() 
Dim htmlDoc As New HtmlAgilityPack.HtmlDocument() 
htmlDoc.LoadHtml(WebSource) 
For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//a[@class]") 
    Dim att As HtmlAttribute = link.Attributes("class") 
    If att.Value = "cellMainLink" Then 
     links.Add(link.Value) 
    End If 
Next 

我不相信你在這裏需要一個正則表達式的解決方案。然而,僅僅用於學習目的:

Dim ptrn As String = "<a\b[^>]*?class=[""']?cellMainLink[""']?[^>]*?>(.*?)</a>" 
Dim input As String = "<a href=""/download/fast-and-furious-7-2015-hd-ts-xvid-ac3-hq-hive-cm8-t10472303.html"" class=""cellMainLink"">**Movie Link 1**</a>" 
Dim dds As List(Of String) = New List(Of String) 
Dim rx As Regex = New Regex(ptrn) 
Dim result As String = rx.Match(input).Groups(1).Value 

結果:**Movie Link 1**

+0

我在看你的HTML敏捷包代碼,但我不知道如何將其實施到我的代碼中。 – SheppoCoder

+0

要麼我得到這些數據的方式是錯誤的,要麼你被誤解了。對不起,不要聽起來粗魯,但只是說我沒有線索如何實現該代碼 – SheppoCoder

相關問題