2013-12-19 2 views
1

나는 (안 사악한 목적을 위해, 그냥 개인적인 프로젝트의) 해적 베이 URL의 첫 번째 링크를 클릭하려고하고 내가 그것을 할 수있는 최선의 방법인지 궁금하네요 :VB.Net에서 링크를 클릭하는 가장 효율적인 방법은 무엇입니까?

For Each ele As HtmlElement In WebBrowser1.Document.Links 

    If ele.GetAttribute("href").Contains("magnet") Then 
     ele.InvokeMember("click") 
     Exit For 
    End If 

Next 

내가 이것이 페이지의 첫 번째 자석 링크를 클릭하는 가장 좋은 방법인지 궁금 합니다만, 현재 웹 브라우저를 사용하고 있습니다. 그러나이를 수행 할 수 있는지 알고 싶습니다. 어쩌면 HTTP 요청이나 그 라인을 따라 무엇인가?

Dim PBsource As String = New System.Net.WebClient().DownloadString("http://pirateproxy.se/search/ubuntu/0/7/0") 
MsgBox(PBsource) 

그러나 아무것도 메시지 박스에 표시되지 않습니다, 그냥 빈입니다, 내가 잘못 URL을 전달하고있다 :

* 편집 GJKH *

에 대한이 같은 코드가?

* 편집 *

2를 내 버튼 하위에서이 코드를 가지고 :

Imports System.Text.RegularExpressions 
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click 

Dim PBsource As String = New System.Net.WebClient().DownloadString("http://pirateproxy.se/search/ubuntu/0/7/0") 
MsgBox(PBsource) 

Dim strReg As String 
'Regex to get a herf links 
strReg = "<a\s+href\s*=\s*""?([^"" >]+)""?>(.+)</a>" 
Dim reg As New Regex(strReg, RegexOptions.IgnoreCase) 
Dim m As Match = reg.Match(PBsource) 
Dim magnetURL As String = "" 
'Keep going while we hit regex matches 
While m.Success 
    If m.Groups(1).Value.ToString.Contains("magnet") Then 
     'Match found, assign magnetURL and exit while 
     magnetURL = m.Groups(1).ToString 
     Exit While 
    End If 
    'Match not found, move to next match 
    m = m.NextMatch() 
End While 


If Not magnetURL Is String.Empty Then 
    Using wc As New System.Net.WebClient 
     wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") 
     PBsource = wc.DownloadString("magnet:?xt=urn:btih:1e4dae83371ba704d5d89e1828068ef0c4151e32&dn=Steam+OS+Official+Installer&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337") 
     MsgBox(PBSource) 
    End Using 
Else 
    MsgBox("no magnet URL found") 
End If 
End Sub 

그러나이 PBSource 올바르게 설정되고 있지 않습니다 보인다 상관없이. 공백 문자열이 생깁니다.

+0

당신은 페이지를 얻을 다음 HTMLAgilityPack를 사용하여 구문 분석 할 수있는 귀하의 코드가 사방에, 내 업데이트 된 대답을 참조 단지 복사되는 링크 –

+0

및 붙여 넣기 "를 클릭". – GJKH

+0

@GJKH 고마워, 나는 원래 여기서부터는 루프를 배우기위한 프로젝트로 시작했다. 그것이 완벽하게 도움이 감사 감사 지금. – SCGB

답변

0

WebClient.DownloadString을 사용하여 HTML을 가져 오는 것이 브라우저를 사용하는 것보다 효율적입니다. 그러면 문자열을 파싱하여 사용자가 얻은 결과를 얻을 수 있습니다.

나는 당신이 어떻게 할 것인가에 대해 완전히 확신 할 수는 없지만, 이론적으로는 다운로드 할 때 데이터를 분석 한 다음 필요로하는 것을 발견하면 작업을 취소 할 수 있습니다. 아마도 과장 될 것입니다.

Using wc As New System.Net.WebClient 
     wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") 
     Dim PBSource = wc.DownloadString("http://pirateproxy.se/search/ubuntu/0/7/0") 

     Dim strReg As String 
     'Regex to get a herf links 
     strReg = "\<a.+?href=(?<q>["" '])(.+?)\k<q>.*?>([^\<]+)" 
     Dim reg As New Regex(strReg, RegexOptions.IgnoreCase) 

     Dim m As Match = reg.Match(PBSource) 

     Dim magnetURL As String = "" 


     'Keep going while we hit regex matches 
     While m.Success 
      If m.Groups(1).Value.ToString.Contains("magnet") Then 
       'Match found, assign magnetURL and exit while 
       magnetURL = m.Groups(1).ToString 
       Exit While 
      End If 
      'Match not found, move to next match 
      m = m.NextMatch() 
     End While 

     If Not magnetURL Is String.Empty Then 
      Dim a = MsgBox("Would you like to open:" & vbCrLf & vbCrLf & magnetURL, MsgBoxStyle.YesNo) 
      If a = MsgBoxResult.Yes Then Process.Start(magnetURL) 
     Else 
      MsgBox("no magnet URLS found") 
     End If 

    End Using 
+0

방금 ​​자석이 아닌 URL로 이것을 테스트하여 저에게 도움이 될 경우 앞뒤에있는'''를 제거해야 할 수도 있습니다 – GJKH

+0

답장을 보내 주셔서 감사합니다. 이전에는 Regex를 사용하지 않았지만 사용해 보려고합니다. 당신은 내가 선두와 후행을 제거해야 할 필요가 있다는 것을 의미합니까? – SCGB

+0

내 테스트에서 일부 URL은 아포스트로피가 있으므로 제거하십시오 - Process.Start (Replace (magnetURL, " '" "")) – GJKH

관련 문제