VB Httpwebresponse 获取内容
VB Httpwebresponse get content
在以下网页上,我想在我的列表框1中获取所有 YouTube 视频的标题
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))
然而,通过这段代码,我得到了标题,还有一些其他的东西...
如果您想坚持使用正则表达式,请尝试以下操作
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
但是专用 API 更干净
YouTube 提供了 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。
要使用 YouTube API,您需要创建一个 API 密钥。这可以从 Google developers console 完成。获得密钥后,您就可以调用 YouTube 来搜索视频、获取视频信息等
以您的代码为基础,您可以使用以下内容:
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
q
参数指定搜索查询。这将获得与您的搜索匹配的前 50 个匹配项,并将它们放入您的下拉列表中。
在以下网页上,我想在我的列表框1中获取所有 YouTube 视频的标题
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))
然而,通过这段代码,我得到了标题,还有一些其他的东西...
如果您想坚持使用正则表达式,请尝试以下操作
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
但是专用 API 更干净
YouTube 提供了 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。
要使用 YouTube API,您需要创建一个 API 密钥。这可以从 Google developers console 完成。获得密钥后,您就可以调用 YouTube 来搜索视频、获取视频信息等
以您的代码为基础,您可以使用以下内容:
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
q
参数指定搜索查询。这将获得与您的搜索匹配的前 50 个匹配项,并将它们放入您的下拉列表中。