2017-10-01 2 views
-1

Ich verwende den folgenden Code, um eine Liste aller Bild-URLs auf einer Webseite zu erhalten. Das Ziel ist, alle Bilder URLs von Google Bildersuche zu bekommen. mein problem, dass ich nur 20 urls und das url-foto bekomme sind die kleinen fotos und nicht die reale größe.Erhalte URLs von google images

hier ist der Code:

public List<string> FetchImages(string Url) 
{ 
    List<string> imageList = new List<string>(); 

    //Append http:// if necessary 
    if (!Url.StartsWith("http://") && !Url.StartsWith("https://")) 
     Url = "http://" + Url; 

    string responseUrl = string.Empty; 
    string htmlData = ASCIIEncoding.ASCII.GetString(DownloadData(Url, out responseUrl)); 

    if (responseUrl != string.Empty) 
     Url = responseUrl; 

    if (htmlData != string.Empty) 
    { 
     string imageHtmlCode = "<img"; 
     string imageSrcCode = @"src="""; 

     int index = htmlData.IndexOf(imageHtmlCode); 
     while (index != -1) 
     { 
      //Remove previous data 
      htmlData = htmlData.Substring(index); 

      //Find the location of the two quotes that mark the image's location 
      int brackedEnd = htmlData.IndexOf('>'); //make sure data will be inside img tag 
      int start = htmlData.IndexOf(imageSrcCode) + imageSrcCode.Length; 
      int end = htmlData.IndexOf('"', start + 1); 

      //Extract the line 
      if (end > start && start < brackedEnd) 
      { 
       string loc = htmlData.Substring(start, end - start); 

       //Store line 
       imageList.Add(loc); 
      } 

      //Move index to next image location 
      if (imageHtmlCode.Length < htmlData.Length) 
       index = htmlData.IndexOf(imageHtmlCode, imageHtmlCode.Length); 
      else 
       index = -1; 
     } 

     //Format the image URLs 
     for (int i = 0; i < imageList.Count; i++) 
     { 
      string img = imageList[i]; 

      string baseUrl = GetBaseURL(Url); 

      if ((!img.StartsWith("http://") && !img.StartsWith("https://")) 
       && baseUrl != string.Empty) 
       img = baseUrl + "/" + img.TrimStart('/'); 

      imageList[i] = img; 
     } 
    } 

    return imageList; 
} 
private string GetBaseURL(string Url) 
{ 
    int inx = Url.IndexOf("://") + "://".Length; 
    int end = Url.IndexOf('/', inx); 

    string baseUrl = string.Empty; 
    if (end != -1) 
     return Url.Substring(0, end); 
    else 
     return string.Empty; 
} 
private byte[] DownloadData(string Url, out string responseUrl) 
{ 
    byte[] downloadedData = new byte[0]; 
    try 
    { 
     //Get a data stream from the url 
     WebRequest req = WebRequest.Create(Url); 
     WebResponse response = req.GetResponse(); 
     Stream stream = response.GetResponseStream(); 

     responseUrl = response.ResponseUri.ToString(); 

     //Download in chuncks 
     byte[] buffer = new byte[1024]; 

     //Get Total Size 
     int dataLength = (int)response.ContentLength; 

     //Download to memory 
     //Note: adjust the streams here to download directly to the hard drive 
     MemoryStream memStream = new MemoryStream(); 
     while (true) 
     { 
      //Try to read the data 
      int bytesRead = stream.Read(buffer, 0, buffer.Length); 

      if (bytesRead == 0) 
      { 
       break; 
      } 
      else 
      { 
       //Write the downloaded data 
       memStream.Write(buffer, 0, bytesRead); 
      } 
     } 

     //Convert the downloaded stream to a byte array 
     downloadedData = memStream.ToArray(); 

     //Clean up 
     stream.Close(); 
     memStream.Close(); 
    } 
    catch (Exception) 
    { 
     responseUrl = string.Empty; 
     return new byte[0]; 
    } 

    return downloadedData; 
    } 
+1

Ich weiß nicht, warum Sie nur 20 Bilder bekommen, aber ich glaube, ich habe eine Antwort für das Erhalten kleiner Bilder: Tatsache ist, dass sie beim Laden der Seite physisch nicht da sind. Wenn Sie das Element auf der Seite überprüfen, werden Sie feststellen, dass die gesamte Seite vollständig mit Javascript bearbeitet wird und dynamisch Bilder und URLs auf Benutzerinteraktionen lädt. Eine leere Seite nach dem Laden enthält wirklich nur kleine Bilder. – Zorak

+0

Ich verstehe es nicht. Schaben Sie Bilder von Google oder einer anderen Website? –

+0

Von Google .. kann jemand andere Bild Suchmaschine wissen? –

Antwort

0

Google scheint nur eine bestimmte Menge von Bildern zu einem Zeitpunkt, zu laden. Wenn Sie auf Ihrem Computer scrollen, wird mehr geladen. Könnte es sein, dass, wenn es ein Bild erreicht, das nicht bevölkert wurde, es das Ende der Bilder betrachtet?

Verwandte Themen