VB Httpwebresponse get content

4.3k views Asked by At

On the following webpage I'd like to get all the titles of youtube videos in my listbox1

        Dim webRequest As WebRequest = webRequest.Create("https://www.youtube.com/results?q=test")
        Dim webresponse As WebResponse = webRequest.GetResponse()

        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(webresponse.GetResponseStream())

        Dim youtube As String = sr.ReadToEnd

        Dim r As New System.Text.RegularExpressions.Regex("title="".*""")
        Dim matches As MatchCollection = r.Matches(youtube)

        For Each itemcode As Match In matches

            ListBox1.Items.Add(itemcode.Value.Split("""").GetValue(1))

However with this code I get the titles but also a bunch of other stuff...

2

There are 2 answers

5
theduck On BEST ANSWER

YouTube provides an API which might be a better way to get this information. The specific call you want to make is documented here: https://developers.google.com/youtube/v3/docs/search/list.

In order to use the YouTube API you will need to create an API key. This can be done from the Google developers console. Once you have a key then you can make calls to YouTube to search videos, get video information etc

Using your code as the basis you could use something along these lines:

Dim url As String = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=test&maxResults=50&key={YOUR-API-KEY}"

Dim webRequest As WebRequest = webRequest.Create(url)
Dim webresponse As WebResponse = webRequest.GetResponse()

Dim sr As System.IO.StreamReader = New System.IO.StreamReader(webresponse.GetResponseStream())

Dim youtube As String = sr.ReadToEnd

Dim r As New System.Text.RegularExpressions.Regex("""title"": "".*""")
Dim matches As MatchCollection = r.Matches(youtube)

For Each itemcode As Match In matches
    ListBox1.Items.Add(itemcode.Value.Split(":").GetValue(1).Trim().TrimStart("""").TrimEnd(""""))
Next

The q parameter specifies the search query. This will get the first 50 matches to your search and put them in your drop down list.

5
sblandin On

If you want to stick with regular expression try the following

Dim r As New System.Text.RegularExpressions.Regex("title=""([^""]*)""")
Dim matches As MatchCollection = r.Matches(youtube)


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

However a dedicated API is cleaner