对 api 进行第一次传递以确定 totalEstimatedMatches。除以 totalEstimatedMatches / 25 或每个页面的大小以获得要进行的 api 调用数。例如,如果 totalEstimatedMatches = 100 则进行 4 次 api 调用,每个调用应返回 25 个 url。我谨慎行事并将其减少 1,但您可以将其放在 try catch 中。本例中的 s.Count 将是 25。VB.Net 中的解决方案,但您明白了。
'the secret key
Dim accessKey As String = "xxxxxxxxxxxxxxxxxxxxxxxxx"
Dim endpoint As String = "https://api.cognitive.microsoft.com/bing/v7.0/news/search?"
Dim queryString = HttpUtility.ParseQueryString(String.Empty)
queryString("q") = search_criteria 'Uri.EscapeDataString(search_criteria)
queryString("mkt") = market
queryString("count") = "25"
queryString("offset") = "0"
queryString("freshness") = freshness
queryString("SafeSearch") = "strict"
' Construct the URI of the search request
uriQuery = endpoint & queryString.ToString
' Perform the Web request and get the response
request = HttpWebRequest.Create(uriQuery)
request.Headers.Add("Ocp-Apim-Subscription-Key", accessKey)
response = CType(request.GetResponseAsync.Result, HttpWebResponse)
json = (New StreamReader(response.GetResponseStream)).ReadToEnd
'create json object
Dim converter = New ExpandoObjectConverter()
Dim message As Object = JsonConvert.DeserializeObject(Of ExpandoObject)(json, converter)
'get top level object and its sub objects
s = message.value
Try
totalEstimatedMatches = CInt(message.totalEstimatedMatches)
total_available_for_processing = s.Count
Catch ex As Exception
End Try
'get total number of pages availble at 25 records per page, so we page thru 25 records at a time and then call api
Dim page_count As Integer = totalEstimatedMatches / 25
'loop thru page_count and
For p As Integer = 0 To page_count - 1
If p = 0 Then
queryString("count") = "25"
queryString("offset") = "0"
Else
'determine offset
queryString("count") = "25"
queryString("offset") = p * 25
End If
' Construct the URI of the search request
uriQuery = endpoint & queryString.ToString
' Perform the Web request and get the response
request = HttpWebRequest.Create(uriQuery)
request.Headers.Add("Ocp-Apim-Subscription-Key", accessKey)
response = CType(request.GetResponseAsync.Result, HttpWebResponse)
json = (New StreamReader(response.GetResponseStream)).ReadToEnd
'create json object
message = JsonConvert.DeserializeObject(Of ExpandoObject)(json, converter)
'get top level object and its sub objects
s = message.value
For i As Integer = 0 To s.Count - 1
Dim myuri As Uri = New Uri(s(i).url.ToString)
Dim vendor_domain As String = myuri.Host
System.Diagnostics.Debug.WriteLine(icount & "," & myuri.ToString & "," & vendor_domain)
icount = icount + 1
Next
System.Threading.Thread.Sleep(100)
Next