/ Published in: VB.NET
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
'http://www.dreamincode.net/code/snippet1249.htm Option Explicit Off Imports System Imports System.IO Imports System.Net Public Class Form1 Dim t1 As Threading.Thread = Nothing Dim t2 As Threading.Thread = Nothing Dim ref() As String Private Sub Btn_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Search.Click t2 = New Threading.Thread(AddressOf search_url) t2.Start() End Sub Private Sub Btn_Download_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Download.Click t1 = New Threading.Thread(AddressOf download_url) t1.Start() End Sub Public Sub download_url() System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False Dim webClient As New System.Net.WebClient Dim i As Integer = 0 Dim url As String Try For i = 0 To 9 url = Me.DataGridView1.Item(2, i).Value webClient.DownloadFile(url, "E:\Balagurunathan\Temp\API\Spider" & i & ".html") Next Catch '' Finally t1.Abort() End Try End Sub Public Sub search_url() System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False Try Dim s As com.google.api.GoogleSearchService = New Sample1.com.google.api.GoogleSearchService() Dim r As com.google.api.GoogleSearchResult = s.doGoogleSearch("Add your Licence Code here", Txt_Text.Text, 0, 10, False, "", True, "", "", "") Dim estimatedCount As Integer = r.estimatedTotalResultsCount Dim dtResults As New DataTable() dtResults.Columns.Add(New DataColumn("Title", GetType(String))) dtResults.Columns.Add(New DataColumn("Summary", GetType(String))) dtResults.Columns.Add(New DataColumn("URL", GetType(String))) For i As Integer = 0 To 50 Dim dr As DataRow = dtResults.NewRow() dr(0) = r.resultElements(i).title dr(1) = r.resultElements(i).snippet dr(2) = r.resultElements(i).URL dtResults.Rows.Add(dr) Next DataGridView1.DataSource = dtResults Catch ex As Exception '' Finally t2.Abort() End Try End Sub Private Sub Btn_Read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Read.Click Me.ListBox1.Items.Clear() For j = 0 To 9 Dim request As WebRequest = WebRequest.Create("E:\Balagurunathan\Temp\API\Spider" & j & ".html") Dim response As WebResponse = request.GetResponse() Dim reader As StreamReader = New StreamReader(response.GetResponseStream()) Dim str As String = reader.Read() Dim myarr() As String = Nothing Dim myarr1() As String = Nothing Do If str.Contains("<a href=") Then GoTo a End If b: If reader.EndOfStream = True Then Exit For Else str = reader.ReadLine() End If Loop Until reader.EndOfStream = True Dim i As Integer = 0 a: For i = 0 To myarr.Length - 1 If myarr(i).Contains("<a href=") Then If Me.ListBox1.Items.Contains(myarr1(1).ToString) = False Then Me.ListBox1.Items.Add(myarr1(1).ToString) Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1 End If End If Next GoTo b Next End Sub End Class