2016-08-17 69 views
0

我試圖下載的HttpWebRequest不檢索我已經爲

https://www.google.com/search?sclient=psy-ab&biw=1472&bih=740&espv=2&tbm=vid&btnG=Search&q=%25%25%25#q=iran&tbm=nws」的網址;

通過下面的代碼:

 string url = "https://www.google.com/search?sclient=psy-ab&biw=1472&bih=740&espv=2&tbm=vid&btnG=Search&q=%25%25%25#q=iran&tbm=nws"; 
     try 
     { 
      string htmlPage = ""; 

      //http request preparing 
      CookieContainer CC = new CookieContainer(); 
      HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); 
      objRequest.Timeout = 60000; 
      objRequest.Proxy = null; 
      objRequest.UseDefaultCredentials = true; 
      objRequest.KeepAlive = false; //THIS DOES THE TRICK 
      objRequest.ProtocolVersion = HttpVersion.Version10; // THIS DOES THE TRICK 
      objRequest.CookieContainer = CC; 

      //http request sending 
      using (HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse()) 
      { 
       using (Stream strm = objResponse.GetResponseStream()) 
       { 
        using (StreamReader objStreamReader = new StreamReader(strm)) 
        { 
         htmlPage = objStreamReader.ReadToEnd(); 
        } 
       } 
      }; 
      if (htmlPage.Contains("No results found for") || htmlPage.Contains("(without quotes):") || htmlPage.Contains("Make sure all words are spelled correctly.")) 
      { 
       return dtResult; 
      } 
      else 
      { 
       Regex objEgEx = new Regex(@"[\r\n][ ]+\.[\r\n][ ]+"); 
       htmlPage = objEgEx.Replace(htmlPage, string.Empty); 

       int startIndex = htmlPage.IndexOf("<div class =\"g\">"); 
       if (startIndex == -1) 
       { Console.Write("problem in parsing"); } 

但HttpWebRequest的下載谷歌的第一頁,而不是我的高清保存它的網址是谷歌搜索結果頁面的視頻搜索服務的地址。 我應該更改什麼,以便下載我想要的網址?

回答

1

您正在下載的頁面,而不是查詢。由於谷歌的搜索不會加載新頁面,但更新頁面。也許看看在谷歌搜索API

+0

我不認爲谷歌更新了同一頁面,因爲我正好唱文本搜索服務和圖片搜索服務中同樣的方法,我可以得到這些服務,但至於結果我儘管查詢字符串中有參數必須導致創建新頁面(或者更新Google的第一頁),但我經常會看到Google的第一頁,而這只是針對視頻而已。 – reza9604

+0

也因爲我計算了谷歌的服務的一些非功能性要求(例如可用性)我無法使用谷歌搜索的API,所以我不能使用的API,因爲如果我這樣做,我會計算statisitcs對於那些我不想要的API – reza9604